Custom Adapter with List View buttons problem

  • Replies:0
Cody Weisenberger
  • Forum posts: 1

Apr 25, 2016, 6:25:32 AM via Website

I am creating a little social network application. I am trying to have a user post something, then be able to like statuses. Currently, if a user likes a status, twoproblems are occurring:
1: When a like button is pressed, it also likes the 'recycled view' button down the list and changes the text of the button on both to unlike, but not like count. ( If user likes the second post in the list, it will also change the text to unlike of other button down the list)
2: The button text will not stay as 'unlike' if the screen is changed.

Here is the custom adapter:

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

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

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

        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")){
            System.out.println("MPOOSTSPOSITION: "+mPosts.get(position));
            mPosts.get(position);
            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);
                                mPosts.get(position).setLikes(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");
            //mPosts.get(position);

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

I would really appreciate any help!

Reply