0

In my GetView() method of my BaseAdapter class... I have a view holder. When I click the holder.feedUpVotebutton The feed.numOFLikes Textview gets incremented. The problem is when I scroll down and scroll back up, the value is back to its original value. I refuse to call notifydatasetChanged(); because I am not adding anything or removing anything from the list.

@Override
public View getView(int i, View view, ViewGroup viewGroup) {

      likes = new int[GlobalFeedTab.arrayFeedList.size()];
      ViewHolder holder;

   if (view == null) {

         position = i;
        view = inflater.inflate(R.layout.feed_list_row, viewGroup, false);         

         holder = new ViewHolder();
         holder.feedNumOfLikes = (TextView) view.findViewById(R.id.feedNumofLikes);
                     holder.feedUpVoteButton = (Button) view.findViewById(R.id.feedUpVoteButton); 
         view.setTag(holder);

      }
   else if(view != null){

        position = i;
        holder = (ViewHolder) view.getTag();

   }

        HashMap<String, String> mFeed = new HashMap<>();   //Hashmap of data that I get from the MainActivity
        mFeed = GlobalFeedTab.arrayFeedList.get(position);

        holder.feedNumOfLikes.setText(mFeed.get("likes"));
         holder.feedUpVoteButton.setTag(position);

          holder.feedUpVoteButton.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {

                            int pos = (Integer) v.getTag();
                           ParseObject parseObFeed = objects.get(pos);                

                                likes[pos] += 1;
                                parseObFeed.put("likes", likes[pos]);
                                holder.feedNumOfLikes.setText(String.valueOf(parseObFeed.getInt("likes")
                                )); //This is where I set the Text

                                parseObFeed.saveInBackground();

             }
        });
     }


 class ViewHolder {

    TextView feedNumOfLikes;
    Button feedUpVoteButton;    

}
grantespo
  • 2,233
  • 2
  • 24
  • 62
  • Why not use ArrayAdapter or even a RecyclerView.Adapter? – OneCricketeer Nov 28 '16 at 22:51
  • `GlobalFeedTab.arrayFeedList` is my ArrayAdapter. @cricket_007 – grantespo Nov 28 '16 at 22:52
  • I don't think so. That's an `ArrayList` – OneCricketeer Nov 28 '16 at 22:54
  • Yes sorry, you are right. The `BaseAdapter` Logic is working perfectly. Everything works except for the issue i listed above. Do you have an idea of how I can fix my problem? @cricket_007 I don't want to use ArrayAdapter or even a RecyclerView.Adapter – grantespo Nov 28 '16 at 22:56
  • Your first problem is that you should not repost your questions. I was going to ask what `objects` was, but your other question is more complete, so I am going to read that instead – OneCricketeer Nov 28 '16 at 22:57
  • Ok, please help. It can't be the most complex thing in the world, I just couldn't find any information on my issue while researching. @cricket_007 – grantespo Nov 28 '16 at 23:06

1 Answers1

0

An Adapter in Android recycles every view, it means that usually your adapter creates only 7~8(actually, it depends on the screen size) views and reuses these views in different position.In your case you just simply need to keep track of state of every position in the adapter.To do so, you can create an array of objects(in your case just an array of integers to keep track of the number of likes for every position on the screen).

This is a brief illustration of mine idea based on your adapter:

  private class YourAdapter extends BaseAdapter {

    private int[] numberOfLikes=new int[getCount()];

    @Override
    public View getView(final int index, View view, ViewGroup viewGroup) {

        likes = new int[GlobalFeedTab.arrayFeedList.size()];
        ViewHolder holder;

        if (view == null) {

            position = i;
            view = inflater.inflate(R.layout.feed_list_row, viewGroup, false);

            holder = new ViewHolder();
            holder.feedNumOfLikes = (TextView) view.findViewById(R.id.feedNumofLikes);
            holder.feedUpVoteButton = (Button) view.findViewById(R.id.feedUpVoteButton);

            holder.feedUpVoteButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {


                    ParseObject parseObFeed = objects.get(pos);
                    likes[pos] += 1;
                    parseObFeed.put("likes", likes[pos]);
                    holder.feedNumOfLikes.setText(String.valueOf(parseObFeed.getInt("likes"))); //This is where I set the Text

                    parseObFeed.saveInBackground();


                }
            });
            view.setTag(holder);

        }
        else if(view != null){

            position = i;
            holder = (ViewHolder) view.getTag();

        }

        HashMap<String, String> mFeed = new HashMap<>();   //Hashmap of data that I get from the MainActivity
        mFeed = GlobalFeedTab.arrayFeedList.get(position);

        holder.feedNumOfLikes.setText("Number of likes:"+Integer.toString(numberOfLikes[index])); //Just assign the value for 

       // holder.feedNumOfLikes.setText(mFeed.get("likes"));
       // holder.feedUpVoteButton.setTag(position); // I don't know why are you doing this

    }



}






class ViewHolder {

    TextView feedNumOfLikes;
    Button feedUpVoteButton;

}

I don't know, maybe you are trying to implement this idea in your code, but I can't see it there. If yes, you need to provide us with more code.

Also you can try using RecyclerView.Adapter which is more convenient. However, then you need to switch from ListView or GridView to RecyclerView which is customizable, and a little bit faster than these above.

nullbyte
  • 1,178
  • 8
  • 16