java - 使用自定义适配器中的变量时遇到问题

标签 java php android android-volley custom-adapter

我正在使用 ListView 并创建自定义适配器。每次点击按钮时我都会尝试给帖子点赞。声明的按钮与 ListView 中的每个项目相关联。我遇到了三个问题:

1)当我点击“赞”按钮时,列表中的“随机”按钮也会被点击。

2)当我点击“喜欢”时,它会更新“喜欢”计数,但是当我向下滚动到 View 之外,然后向上滚动时,“喜欢”计数将被重置。

3)当我点击“赞”时,“赞”计数将会改变,现在当我在另一个帖子上点击“赞”时,“赞”计数将更改为我单击的第一个“赞”按钮上的“赞”计数。

我已经被困在这个问题上有一段时间了,非常感谢任何帮助。

这是适配器类的代码:

    class PostsAdapter extends ArrayAdapter<Posts> implements View.OnClickListener {
    //used to create views from xml
    private LayoutInflater layoutInflater;

    public PostsAdapter(Context context, int textViewResourceId, List<Posts> posts) {
        super(context, textViewResourceId, posts);
        layoutInflater = LayoutInflater.from(context);
    }
    //add to xml from dataset
    @Override
    // view convertview = recycled view
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        Holder holder = null;
        Posts posts = getItem(position);

        //checks if recycled view is null, thewn creates new view, if not null, use same view
        if(view == null){
            view = layoutInflater.inflate(R.layout.activity_posts, null);

            Button like = (Button) view.findViewById(R.id.btnLike);
            TextView content = (TextView) view.findViewById(R.id.content);
            TextView user = (TextView) view.findViewById(R.id.user);
            TextView topic = (TextView) view.findViewById(R.id.topic);
            TextView date = (TextView) view.findViewById(R.id.date);
            TextView likes = (TextView) view.findViewById(R.id.likeCount);
            TextView hiddenId = (TextView) view.findViewById(R.id.hiddenID);

            holder = new Holder(content, user, topic, date, likes, hiddenId, like);

            view.setTag(holder);
            like.setTag(holder);
        }
        else{
            holder = (Holder) view.getTag();
        }
        holder.content.setText(posts.getContent());
        holder.user.setText(posts.getUser());
        holder.topic.setText(posts.getTopic());
        holder.date.setText(posts.getDate());
        holder.likes.setText(posts.getLikes());
        holder.hiddenId.setText(posts.getId());

        holder.like.setOnClickListener(this);

        return view;
    }

    @Override
    public void onClick(View v) {
        Button like = (Button) v;
        Holder holder = (Holder) v.getTag();
        Toast.makeText(DisplayPosts.this, "Button: " + like.getText(), Toast.LENGTH_LONG).show();

        createLike(holder.like, holder.hiddenId, holder.likes);
    }
}

static class Holder{
    public TextView content;
    public TextView user;
    public TextView topic;
    public TextView date;
    public TextView likes;
    public TextView hiddenId;
    public Button like;

    public Holder(TextView content, TextView user, TextView topic, TextView date, TextView likes, TextView hiddenId, Button like) {
        this.content = content;
        this.user = user;
        this.topic = topic;
        this.date = date;
        this.likes = likes;
        this.hiddenId = hiddenId;
        this.like = like;
    }
}

这是 createLike 方法:

    private void createLike(final Button like, final TextView hiddenid, final TextView likes){
    final String hid = hiddenid.getText().toString().trim();

    if(like.getText().toString().equalsIgnoreCase("like")){
        like.setText("UnLike");
        StringRequest stringRequest = new StringRequest(Request.Method.POST, Config.SERVER_ADDRESS + "LikePost.php",
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {

                        JSONObject jsonObject = null;
                        try {
                            //json string to jsonobject
                            jsonObject = new JSONObject(response);
                            //get json sstring created in php and store to JSON Array
                            result2 = jsonObject.getJSONArray(Config.json_array_likes);
                            //get username from json array
                            likes.setText(getLikeCount(result2));
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                    }
                }){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {

                // corresponding values.
                Map<String,String> hashMap = new HashMap<>();
                //maps specified string key, to specified string value
                hashMap.put(Config.hid, hid);
                return hashMap;
            }
        };

        //add string request to queue
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(stringRequest);
    }
    else{
        like.setText("Like");
    }
}
private String getLikeCount(JSONArray jsonArray){
    String lc = null;
    for(int i = 0; i < jsonArray.length(); i++) {
        try {
            JSONObject json = jsonArray.getJSONObject(i);
            likeCount.add(json.getString(Config.getLike));
            lc = likeCount.get(0);
        } catch (JSONException e) {
        }
    }
    return lc;
}

最佳答案

我认为根本问题是您在持有者类上使用静态修饰符,删除它,我相信它会解决您的问题。

关于java - 使用自定义适配器中的变量时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36655370/

相关文章:

Java、带有 MySQL 的 LDAP 以及用户身份验证

java - 无法在 Spring MVC @Controller 类中访问 HttpSession

php - FFMPEG 脚本不在 php 中运行

php - php中的run_init和load::om是什么

iphone - 我应该使用哪个威瑞信 SSL 证书?

java - Java 从 6.21 更新到 6.31 后,应用程序无法正常工作

Java mkdir -p 等效项

java - 在 Android 上没有工具栏可以制作视差效果吗?

php - Laravel: Eloquent 关系创造多

android - '转换为 Dalvik 格式失败,错误为 1' and ' java.lang.IllegalArgumentException : already added: '