Android Imageview In Listview Onclick Change Image Doesn't Work Properly
Solution 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:
privateboolean[] 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);
Solution 2:
privateint selectedPositions ;
// /... your code.
convertView.setOnClickListener(new OnClickListener() {
@Override
publicvoidonClick(View v) {
OneComment mComment = mlist.get(position);
mComment.isStart = !mComment.isStart;
if(mComment.isStar){
//set star image
} else{
donotset star image}
}
});
Solution 3:
@semsamot's answer works, however
notifyDataSetInvalidated()
causes the List to reload and goes to the first item.
Use notifyDataSetChanged()
instead.
Post a Comment for "Android Imageview In Listview Onclick Change Image Doesn't Work Properly"