I have a ListView which I populate using a custom CursorAdapter. Now I want to manually update just one specific item in the ListView. I have the content URI of that item. Is it possible to use just this info to get the position of the item in the listView? If I have the position I can do something like
View v = mListView.getChildAt(itemPosition - mListView.getFirstVisiblePosition());
to update the view. But how can I get itemPosition?
I know that I get the position in the onItemClickListener, but I need to update the view without it being clicked.
Any help guys?
ListViewdoes not support updating a single position.You must update the adapter with the new data (even if you change a single position) and the, invoke
mAdapter.notifyDataSetChanged()If you get the
Viewby its position (vialistView.getChildAt()) will work. However, if you scroll up and down the list, that view will display the old data again because the adapter is not aware of the change (and it is the adapter which update the view contect viewgetView()).When you invoke
notifyDataSetChanged(), you are telling to the adapter that your data set has new info and theListView/Adapterwill re-draw the visible items (it won't re-draw whole list at once.. only the visible items).You may want to consider to change to
RecyclerViewin the future. TheBaseAdapterused in aRecyclerViewsupport actions such as add/remove/update a single position.