How to refresh an Android ListView
after adding/deleting dynamic data?
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
If you are using SimpleCursorAdapter try calling requery() on the Cursor object.
I was the same when, in a fragment, I wanted to populate a ListView (in a single TextView) with the mac address of BLE devices scanned over some time.
What I did was this:
Then the ListView began to dynamically populate with the mac address of the devices found.
If you want to update the UI listview from a service, then make the adapter static in your Main activity and do this:
If I talked about my scenario here, non of above answers will not worked because I had activity that show list of db values along with a delete button and when a delete button is pressed, I wanted to delete that item from the list.
The cool thing was, I did not used recycler view but a simple list view and that list view initialized in the adapter class. So, calling the
notifyDataSetChanged()
will not do anything inside the adapter class and even in the activity class where adapter object is initialized because delete method was in the adapter class.So, the solution was to remove the object from the adapter in the adapter class
getView
method(to only delete that specific object but if you want to delete all, callclear()
).To you to get some idea, what was my code look like,
Note: here
remove()
andgetItem()
methods are inherit from the Adapter class.remove()
- to remove the specific item that is clickedgetItem(position)
- is to get the item(here, thats my Word object that I have added to the list) from the clicked position.This is how I set the adapter to the listview in the activity class,
I think it depends on what you mean by refresh. Do you mean that the GUI display should be refreshed, or do you mean that the child views should be refreshed such that you can programatically call getChildAt(int) and get the view corresponding to what is in the Adapter.
If you want the GUI display refreshed, then call notifyDataSetChanged() on the adapter. The GUI will be refreshed when next redrawn.
If you want to be able to call getChildAt(int) and get a view that reflects what is what is in the adapter, then call to layoutChildren(). This will cause the child view to be reconstructed from the adapter data.
Also you can use this: