0

In my layout I have a button and a listview. How can I change the imageview of item I selected when the button is clicked. So lets say, I select 5 items, and after I click the button, images for those 5 items will be changed.

So I am confused what function I should use. Right now i used button.setOnClickListener but it seems wrong because only the very first item's imageview will be changed when button clicked. Should I use listview.setItemOnClickListener? Or is there any other way I can do this?

Thanks a lot!

3 Answers3

0

You can used custom baseadapter for listview and setonclicklistener in the getView() method.... see tutorial here this is hope for help

Community
  • 1
  • 1
Myo Min Han
  • 1,633
  • 2
  • 12
  • 12
  • I think that is the case when you have multiple buttons, and buttons are within the listview, right? I only have one button and it is outside of my listview, but in the same layout. Are they the same? Thanks – user3799934 Jul 12 '14 at 22:25
0

Add a boolean to the data object in your adapter. Say you've got ArrayAdapter<MyDataObject>. Add some kind of "selected" field in the MyDataObject, and toggle it when you "select" the row.

Override getView in the adapter (you'll need a custom Adapter, btw. I'd just extend ArrayAdapter). When you render the row, if the "selected" field is true, show the "other" image.

When you click the button, call 'notifyDataSetChanged' on the adapter. The will cause the visible rows to refresh themselves (and call getView for each).

I think that'll work.

Since you only want images to change when you click the button, you'll need to have some kind of global boolean, so the getView won't show the image until the button has been clicked.

The complication here is, you have to deal with rows that may have been scrolled out of view, which don't have active views, but logically exist. It would be really hard to explain the concept here. I'd suggest some tutorials on ListView if you're not familiar with the recycling of row views.

Kevin Galligan
  • 16,159
  • 5
  • 42
  • 62
0

You can use button click listener as well as on itemClickListener too but to make a image view in selected state in a list, you have to call setSelected method of imageview parent layout.

Please put a comment if you don't get me.

Thanks

Ankur Chaudhary
  • 2,709
  • 3
  • 19
  • 30