I have a requirement, to toggle the image of a listview item(s) from a button located in the header section of the view.
So I have a listview that uses a custom BaseAdapter, the adapter is pretty basic, it has an ImageView and a TextView per item.
The header section will have 2 buttons, each button will have a package associated to it. For example.
Button A will have PACKAGE {A=Product 1, C=Product 3, G=Product 7}
Button B will have PACKAGE {B=Product 2, D=Product 4, G=Product 7}
The Listview Items will have the following items along with an image.
{A=Product 1, B=Product 2,
C=Product 3, D=Product 4,
E=Product 5, F=Product 6, G=Product 7}
Simulated Listview
image green - A Product 1
image green - B Product 2
image green - C Product 3
image green - D Product 4
image green - E Product 5
image green - F Product 6
image green - G Product 7
If the user select’s button “A” the images of the listview items should change from red to green
only for the items that are in button “A” package list. So for this example, only listview
items A, C and G will have their image items changed to GREEN.
How can I update the listview items without actually having the user having to click on the listview item?
I suppose you have implemented the
OnClickListenerfor your 2 buttons.Now when clicking on one the buttons you save what kind of button you clicked inside a global variable.
After that call notifyDataChanged() on you
Adapter.When notifyDataChanged() is called the
getView(int position, View convertView, ViewGroup parent)of yourAdapteris called.The getView method is called for every visible item in your ListView.
Now inside the getView method you simply just switch the images on whether it’s button A that were clicked or button B that were clicked.
For an example on how to customize your
Adapterfor theListViewhave a look at this post: http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/