Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update Android ListActivity when list data changes?

I wanted to know how to refresh a ListActivity when I change/add data to it.

I first thought that ListAdapter would know when the list is changes, but when I have added elements to the list there is no update on the ListActivity, only when I close the ListActivity and reopen it again I see the changes occur.

So I searched for any method such as: update(), refesh() or something like that, but there is none. It seems I haven't gotten the concept, can someone help me please?

like image 561
Simon Avatar asked Oct 08 '09 16:10

Simon


People also ask

How do I update android lists?

This example demonstrates how do I dynamically update a ListView in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

What is notifyDataSetChanged in android?

notifyDataSetChanged. Notifies the attached observers that the underlying data has been changed and any View reflecting the data set should refresh itself.

What does setAdapter do?

In order to display items in the list, call setAdapter(android. widget. ListAdapter) to associate an adapter with the list. For a simple example, see the discussion of filling an adapter view with text in the Layouts guide.


1 Answers

If you are using an ArrayAdapter, manipulate the contents of the list through the adapter -- do not touch the underlying list. There are add(), insert(), and remove() methods on ArrayAdapter.

If you are using a CursorAdapter, run a requery() on the Cursor.

like image 150
CommonsWare Avatar answered Oct 22 '22 02:10

CommonsWare