Li Che
Li Che

Reputation: 737

change to another image when click imageview

I set an image A for imageview of item in listview. I set the onclick event and change to another image B. But after clicking it can not change to another iamge. When I print Log I see this method has been called.

holder.bus_icon.setImageResource(R.drawable.bus_blue);
            holder.bus_icon.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.i("i", "点击图标了+test11" + position);
                    holder.bus_icon.setImageResource(R.drawable.bus_red);
                    adapter.notifyDataSetChanged();
                }
            });

Upvotes: 0

Views: 1684

Answers (2)

Faizan
Faizan

Reputation: 3512

In the onclick, the holder is pointing to wrong imageView.

Use this instead

               @Override
                public void onClick(View v) {
                    Log.i("i", "点击图标了+test11" + position);
                    ImageView iv = (ImageView)v;
                    iv.setImageResource(R.drawable.bus_red);
                }

Hope it solves

Upvotes: 2

Chirag
Chirag

Reputation: 56935

Remove adapter.notifyDataSetChanged(); because you nothing changed in adapter and call holder.bus_icon.invalidate();.

Hope this may help you.

Upvotes: 3

Related Questions