简体   繁体   中英

Android clicking on one ListView item affect other ListView items?

My ListView item consists of the following components - TextView and then under it there are two ImageViews - like and dislike .

So when I click on either like or dislike I want to be able to change the like or dislike ImageView from grey to blue.

At the moment when I click on like the like ImageView does change from grey to blue. But not only for the corresponding ListView item but for every 3rd item in the list - so if I have 10 items in my list and I click on the like of the first item in the list then 4th, 7th and 10th items like ImageView change from grey to blue.

In my post_list_item.xml in the root element of the file I specified the following android:descendantFocusability="blocksDescendants" but it doesn't help either.

My question is - what do I have to do so that when I click on either like or dislike I would be able to change the like or dislike ImageView from grey to blue without affecting other list items?

Here is my code of the CursorAdapter

public class PostCursorAdapter extends CursorAdapter {

    public PostCursorAdapter(Context context, Cursor c, int flags) {
        super(context, c, flags);
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        return LayoutInflater.from(context).inflate(R.layout.post_list_item, parent, false);
    }

    @Override
    public void bindView(View view, final Context context, Cursor cursor) {
        String post = cursor.getString(cursor.getColumnIndex(DBOpenHelper.POST));
        final String liked = cursor.getString(cursor.getColumnIndex(DBOpenHelper.LIKED));
        String disliked = cursor.getString(cursor.getColumnIndex(DBOpenHelper.DISLIKED));
        final String post_id = cursor.getString(cursor.getColumnIndex(DBOpenHelper.POST_ID));


String userliked = cursor.getString(cursor.getColumnIndex(DBOpenHelper.USER_LIKED));


        TextView tvPost = (TextView) view.findViewById(R.id.tvPost );
        tvJoke.setText(post);

        TextView tvLikeCount = (TextView) view.findViewById(R.id.tvLikeCount);
        tvLikeCount.setText(liked);

        TextView tvDislikesCount = (TextView) view.findViewById(R.id.tvDislikeCount);
        tvDislikesCount.setText(disliked);

        final ImageView ivLike = (ImageView) view.findViewById(R.id.ivLike);
// has the user liked it?
int check = Integer.parseInt(userliked);
if(check == 1){
    String uri = "@drawable/like_blue";  // where myresource (without the extension) is the file
    int imageResource = context.getResources().getIdentifier(uri, null, context.getPackageName());
    Drawable res = context.getResources().getDrawable(imageResource);
    ivLike.setImageDrawable(res);
}
        ivLike.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String uri = "@drawable/like_blue"; 
                int imageResource = context.getResources().getIdentifier(uri, null, context.getPackageName());
                Drawable res = context.getResources().getDrawable(imageResource);
                ivLike.setImageDrawable(res);

            }
        });
    }

}

You can use state drawables in your drawables/like.xml like this,

like.xml

    <item android:state_activated="true"
        android:drawable="@drawable/like_blue"/>
    <item android:drawable="@drawable/like_grey"/>

you can add to your imageview's

android:src="@drawable/like"
android:clickable="true"

in your adapter,

   ivLike.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

          if(ivLike.isActivated())
               ivLike.setActivated(false);
          else
               ivLike.setActivated(true);
         }
    });

for dislike image also you can do the same.

You are running into a view recycling issue. To learn more, look at the answer to this question . In your case, the easiest thing to do is to use the view that's passed into your onClick method:

    ivLike.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            ImageView icon = (ImageView) view;
            String uri = "@drawable/like_blue"; 
            int imageResource = context.getResources().getIdentifier(uri, null, context.getPackageName());
            Drawable res = context.getResources().getDrawable(imageResource);
            icon.setImageDrawable(res);

        }
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM