Custom Adapter with List View buttons problem

  • Replies:0
  • OpenNot stickiedUnanswered
  • 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
    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(;
            TextView content = (TextView) view.findViewById(;
            TextView user = (TextView) view.findViewById(;
            TextView topic = (TextView) view.findViewById(;
            TextView date = (TextView) view.findViewById(;
            TextView likes = (TextView) view.findViewById(;
            TextView hiddenId = (TextView) view.findViewById(;
            holder = new Holder(content, user, topic, date, likes, hiddenId, like);

   View.OnClickListener() {
                public void onClick(View v) {
                    createLike(position,, holder.hiddenId, holder.likes);//Added one more parameter "position"

            holder = (Holder) view.getTag();

        Posts posts = mPosts.get(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();

            System.out.println("MPOOSTSPOSITION: "+mPosts.get(position));
            StringRequest stringRequest = new StringRequest(Request.Method.POST, Config.SERVER_ADDRESS + "LikePost.php",
                    new Response.Listener<String>() {
                        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
                                String likestring = getLikeCount(result2);
                                String likenum = mPosts.get(position).getLikes(likestring);

                            } catch (JSONException e) {
                    new Response.ErrorListener() {
                        public void onErrorResponse(VolleyError error) {
                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);

    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);
                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;
   = date;
            this.likes = likes;
            this.hiddenId = hiddenId;
   = like;

I would really appreciate any help!