0

I have an android app in which users can like and unlike an image.I'm using recyclerView.I Just disable the button(Like/Unlike) once user clicked. Problem, when I click on button like , the apps go to main activity and the button Like doesn't change to unlike What I have done :

1 ) layout that holds the each recycler view layout item

2 ) A view holder for creating each layout

3 ) A Model Class to holds the data

4 ) Recycler Adaptor which deals with the data for the Each Layout item

Hier ist my view holder

//Initializing Views
        public ViewHolder(View itemView) {
            super(itemView);
            imageView = (NetworkImageView)      itemView.findViewById(R.id.imageViewHero);
            textViewName = (TextView) itemView.findViewById(R.id.textViewName);
            //textViewPublisher = (TextView) itemView.findViewById(R.id.textViewPublisher);
            likeImageView = (ImageView) itemView.findViewById(R.id.likeImageView);
            likeImageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {


                    int id = (int)likeImageView.getTag();
                    if( id == R.drawable.ic_like){

                        likeImageView.setTag(R.drawable.ic_liked);
                        likeImageView.setImageResource(R.drawable.ic_liked);                       

                    }else{

                        likeImageView.setTag(R.drawable.ic_like);
                        likeImageView.setImageResource(R.drawable.ic_like);               


                    }

                }
            });
apaderno
  • 28,547
  • 16
  • 75
  • 90

1 Answers1

0

I answered this type of questions over and over. You didn't search enough and you didn't understand how ListView or RecycleView works. Changing current state of views ( such as changing text of TextView or changing resource of ImageView) is the wrong thing. You need data set (a list related to items in ListView) and you need to change corresponding data of the list and call notifyDataSetChanged() method of your adapter.

Don't forget. getView() method of your adapter is called every time any view of your list become on the screen and if you update only the view (instead of change data) your view will show the past value of item because your data didn't changed.

Look link below and search much more about how ListView and RecycleView works.

How ListView's recycling mechanism works

Efe AYDIN
  • 193
  • 12