无法使用自定义适配器中的变量

时间:2016-04-15 19:26:46

标签: java php android android-volley custom-adapter

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

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

2)当我点击时,它会更新相似的数量,但是当我向下滚动到视图外,然后向上滚动,重置类似的计数。

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;
}

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

而不是

holder.like.setOnClickListener(this);

试试这个 -

holder.like.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                createLike(position,holder.like, holder.hiddenId, holder.likes);//Added one more parameter "position"
             }
);

现在在createLike(),你必须使用“posts”项目的新计数来更新数据集“position” -

                        @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));

//Update like count here
posts.get(position).setLikeCount(); //The count which you want to update

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }

答案 2 :(得分:0)

更新:

    public class PostsAdapter extends ArrayAdapter<Posts>  {
    //used to create views from xml
    Context context;
    int textViewResourceId;
    ArrayList<Posts> mPosts = new ArrayList<Posts>();

    public PostsAdapter(Context context, int textViewResourceId, ArrayList<Posts> posts) {
        super(context, textViewResourceId, posts);
        this.textViewResourceId = textViewResourceId;
        this.context = context;
        this.mPosts = posts;
    }

    // view convertview = recycled view
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View view = convertView;
        final Holder holder;
        //checks if recycled view is null, thewn creates new view, if not null, use same view
        if(view == null){
            LayoutInflater inflater = ((Activity) context).getLayoutInflater();
            view = inflater.inflate(textViewResourceId, parent, false);

            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);
        }
        else{
            holder = (Holder) view.getTag();
        }

        Posts posts = mPosts.get(position);
        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(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                createLike(position, holder.like, holder.hiddenId, holder.likes);//Added one more parameter "position"
            }
        });

        return view;
    }

    private void createLike(final int position, 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 {
                                //counter
                                n+=1;

                                //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
                                String likestring = getLikeCount(result2);
                                String likenum = mPosts.get(position).getLikes(likestring);
                                likes.setText(likenum);

                            } 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(DisplayPosts.this);
            requestQueue.add(stringRequest);
        }
        else{
            like.setText("Like");
        }
    }
    private String getLikeCount(JSONArray jsonArray){
        System.out.println("JSON: " + jsonArray);
        String lc = null;
        for(int i = 0; i < jsonArray.length(); i++) {
            try {
                JSONObject json = jsonArray.getJSONObject(i);
                likeCount.add(json.getString(Config.getLike));
                System.out.println("i: " + i);
                lc = likeCount.get(n - 1);
            } catch (JSONException e) {
            }
        }
        System.out.println("LC: " + lc);
        return lc;
    }
     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;
        }
    }
}