Android - updating the simpleCursorAdapter
I have a ListActivity
which gets its data from a database query. I also have a custom adapter which extends simple cursor adapt开发者_StackOverflower.
To show the customised content, I have overridden the newView
and bindView methods.
Each element of the view has:
TextView
containing a titleImageView
containing number of stars - the image shown is changed based on a value obtained from the database- A button - the button text changes on clicks (Favourite/Make Favourite), and triggers a database update event.
My problem is this - when I scroll the ListView
, the changes I made seem to disappear .. for instance, the first item is marked favourite, and the list is scrolled ... when I come back to the first item, the text reverts back to its previous value, although internally the db has been updated.
I read that the notifyDatasetChanged()
is not right for this case as the adapter should be notified of the database changes. I am trying to use the reQuery()
method, but do not know where to place it.
Should I place the reQuery()
in the onClick
method of the button? If not, where should it be placed?
What so ever Adapter is you are using must have getView Method.. Whenever a list is scrolled this getView Method got called for all the listItem in the view. Make Sure in the getView Method. That the view is getting generated dynamically.
It should check for clicked or changed value.
to update the list without scrolling your have to call listView.getAdapter().notifyDataSetChanged()
Check the following link. here i mentioned a complete code to use listview properly. Using this we can achieve any listview behavior. We can embed animation also.
Change ListView background - strange behaviour
Hope this help :)
If you have extended BaseAdapter , after data is changed call , listView.setAdapter() and in getView() , update the view's data too.
Use SetListAdapter
with ArrayAdapter
to display the list. Use ConvertView
to avoid inflating View
.
if (convertView==null)
{
//inflate your list
}
精彩评论