Android Imageview in Listview onClick change image doesn't work properly

心已入冬 提交于 2019-12-11 08:51:40

问题


I have a ListView and in every single ListviewItem there is an ImageView with a little star (for marking it as favourite). Therefore I put an OnClickListener to the ImageView on every item in the custom ArrayAdapter.

imgStar.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Bitmap bitmap = ((BitmapDrawable)imgStar.getDrawable()).getBitmap();
            Bitmap bitmap2 = ((BitmapDrawable)(context.getResources().getDrawable(R.drawable.ic_action_not_important))).getBitmap();

            if(bitmap != bitmap2) {
                    imgStar.setImageResource(R.drawable.ic_action_not_important);
            } else {
                    imgStar.setImageResource(R.drawable.ic_action_important);
            }
        }
    });

The problem: When I get some items and click for example on the star of the first item, the image changes as it should but a few items lower the image changes too o.O I tested it with some code: The thing that won't get into my head is it is only changing the image (on the other item below), code that would be executed in the onclick is only executed for the item I really click not for the one where the image changes too.

Why does the image of a random other item in the list change also? I hope someone can help me.

Custom Adapter Constructore Code

public LinkArrayAdapter(Context con, int textViewResourceId) {
    super(con, textViewResourceId);
    context = con;

}

回答1:


The main problem is that you can't change the image of items in the onClick then leave it and hope it will be updated on every item on the list. Because onClick get called in different time than getView. So you must set item images outside of onClick but in the getView so every time that getView called for a specific item it will set the appropriate image for that item.

Define a boolean array in your CustomAdapter class as:

private boolean[] stars;

Then in constructor method of your class, initialize it as:

this.stars = new boolean[items.size()];

In the onClick method:

// **Edited to apply image update at click**
stars[position] = !stars[position];
notifyDataSetInvalidated();

At last in the getView() method of custom adapter (ensure this code is not in any other inner blocks):

if (stars[position])
  imgStar.setImageResource(R.drawable.ic_action_important);
else
  imgStar.setImageResource(R.drawable.ic_action_not_important);



回答2:


    private int  selectedPositions ;

    // /... your code.
            convertView.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {

                    OneComment mComment = mlist.get(position);
                    mComment.isStart = !mComment.isStart;
                    if(mComment.isStar){
//set star image
} else{
do not set star image}

                }
            });



回答3:


@semsamot's answer works, however notifyDataSetInvalidated() causes the List to reload and goes to the first item. Use notifyDataSetChanged() instead.



来源:https://stackoverflow.com/questions/25403293/android-imageview-in-listview-onclick-change-image-doesnt-work-properly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!