How to refresh an Android ListView after adding/deleting dynamic data?

Solution 1

Call notifyDataSetChanged() on your Adapter object once you've modified the data in that adapter.

Some additional specifics on how/when to call notifyDataSetChanged() can be viewed in this Google I/O video.

Solution 2

Also you can use this:


Solution 3

Please ignore all the invalidate(), invalidateViews(), requestLayout(), ... answers to this question.

The right thing to do (and luckily also marked as right answer) is to call notifyDataSetChanged() on your Adapter.


If calling notifyDataSetChanged() doesn't work all the layout methods won't help either. Believe me the ListView was properly updated. If you fail to find the difference you need to check where the data in your adapter comes from.

If this is just a collection you're keeping in memory check that you actually deleted from or added the item(s) to the collection before calling the notifyDataSetChanged().

If you're working with a database or service backend you'll have to call the method to retrieve the information again (or manipulate the in memory data) before calling the notifyDataSetChanged().

The thing is this notifyDataSetChanged only works if the dataset has changed. So that is the place to look if you don't find changes coming through. Debug if needed.

ArrayAdapter vs BaseAdapter

I did find that working with an adapter that lets you manage the collection, like a BaseAdapter works better. Some adapters like the ArrayAdapter already manage their own collection making it harder to get to the proper collection for updates. It's really just an needless extra layer of difficulty in most cases.

UI Thread

It is true that this has to be called from the UI thread. Other answers have examples on how to achieve this. However this is only required if you're working on this information from outside the UI thread. That is from a service or a non UI thread. In simple cases you'll be updating your data from a button click or another activity/fragment. So still within the UI thread. No need to always pop that runOnUiTrhead in.

Quick Example Project

Can be found at Just clone and open the project in Android Studio (gradle). This project has a MainAcitivity building a ListView with all random data. This list can be refreshed using the action menu.

The adapter implementation I created for this example ModelObject exposes the data collection

public class MyListAdapter extends BaseAdapter {

     * this is our own collection of data, can be anything we 
     * want it to be as long as we get the abstract methods 
     * implemented using this data and work on this data 
     * (see getter) you should be fine
    private List<ModelObject> mData;

     * our ctor for this adapter, we'll accept all the things 
     * we need here
     * @param mData
    public MyListAdapter(final Context context, final List<ModelObject> mData) {
        this.mData = mData;
        this.mContext = context;

    public List<ModelObject> getData() {
        return mData;

    // implement all abstract methods here

Code from the MainActivity

public class MainActivity extends Activity {

    private MyListAdapter mAdapter;

    protected void onCreate(Bundle savedInstanceState) {

        ListView list = (ListView) findViewById(;

        // create some dummy data here
        List<ModelObject> objects = getRandomData();
        // and put it into an adapter for the list
        mAdapter = new MyListAdapter(this, objects);

        // mAdapter is available in the helper methods below and the 
        // data will be updated based on action menu interactions

        // you could also keep the reference to the android ListView 
        // object instead and use the {@link ListView#getAdapter()} 
        // method instead. However you would have to cast that adapter 
        // to your own instance every time

     * helper to show what happens when all data is new
    private void reloadAllData(){
        // get new modified random data
        List<ModelObject> objects = getRandomData();
        // update data in our adapter
        // fire the event

     * helper to show how only changing properties of data 
     * elements also works
    private void scrambleChecked(){
        Random random = new Random();
        // update data in our adapter, iterate all objects and 
        // resetting the checked option
        for( ModelObject mo : mAdapter.getData()) {
        // fire the event

More Information

Another nice post about the power of listViews is found here:

Solution 4

Call runnable whenever you want:


OnCreate(), you set your runnable thread:

run = new Runnable() {
    public void run() {
        //reload content

Solution 5

i got some problems with dynamic refresh of my listview.

Call notifyDataSetChanged() on your Adapter.

Some additional specifics on how/when to call notifyDataSetChanged() can be viewed in this Google I/O video.

notifyDataSetChanged() did not work properly in my case[ I called the notifyDataSetChanged from another class]. Just in the case i edited the ListView in the running Activity (Thread). That video thanks to Christopher gave the final hint.

In my second class i used

Runnable run = new Runnable(){
     public void run(){

to acces the update() from my Activity. This update includes


to tell the Adapter to refresh the view. Worked fine as far as I can say.

Solution 6

If you are using SimpleCursorAdapter try calling requery() on the Cursor object.

Solution 7

if you are not still satisfied with ListView Refreshment, you can look at this snippet,this is for loading the listView from DB, Actually what you have to do is simply reload the ListView,after you perform any CRUD Operation Its not a best way to code, but it will refresh the ListView as you wish..

It works for Me....if u find better solution,please Share...

do your CRUD Operations..
// Bring that DB_results and add it to list as its contents....
                                            ls2.setAdapter(new ArrayAdapter(DynTABSample.this,
    android.R.layout.simple_list_item_1,                        DBAdapter.DB_ListView));

Solution 8

The solutions proposed by people in this post works or not mainly depending on the Android version of your device. For Example to use the AddAll method you have to put android:minSdkVersion="10" in your android device.

To solve this questions for all devices I have created my on own method in my adapter and use inside the add and remove method inherits from ArrayAdapter that update you data without problems.

My Code: Using my own data class RaceResult, you use your own data model.

public class ResultGpRowAdapter extends ArrayAdapter<RaceResult> {

    Context context;
    int resource;
    List<RaceResult> data=null;

        public ResultGpRowAdapter(Context context, int resource, List<RaceResult> objects)           {
        super(context, resource, objects);

        this.context = context;
        this.resource = resource; = objects;

    public View getView(int position, View convertView, ViewGroup parent) {


        //my own method to populate data           
        public void myAddAll(List<RaceResult> items) {

        for (RaceResult item:items){

public class ResultsGp extends Activity {

protected void onCreate(Bundle savedInstanceState) {

    ListView list = (ListView)findViewById(; 

    ResultGpRowAdapter adapter = new ResultGpRowAdapter(this,  R.layout.activity_result_gp_row, new ArrayList<RaceResult>()); //Empty data


   //LOAD a ArrayList<RaceResult> with data

   ArrayList<RaceResult> data = new ArrayList<RaceResult>();
   data.add(new RaceResult(....));
   data.add(new RaceResult(....));

   adapter.myAddAll(data); //Your list will be udpdated!!!

Solution 9

For me after changing information in sql database nothing could refresh list view( to be specific expandable list view) so if notifyDataSetChanged() doesn't help, you can try to clear your list first and add it again after that call notifyDataSetChanged(). For example

private List<List<SomeNewArray>> arrayList;
List<SomeNewArray> array1= getArrayList(...);
List<SomeNewArray> array2= getArrayList(...);

Hope it makes sense for you.

Solution 10

If you want to maintain your scroll position when you refresh, and you can do this:

if (mEventListView.getAdapter() == null) {
    EventLogAdapter eventLogAdapter = new EventLogAdapter(mContext, events);
} else {

public void refill(List<EventLog> events) {

For the detail information, please see Android ListView: Maintain your scroll position when you refresh.

Solution 11

Just use myArrayList.remove(position); inside a listener:

  myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override public void onItemClick(AdapterView<?> parent, android.view.View view, int position, long id) {

Solution 12

You need to use a single object of that list whoose data you are inflating on ListView. If reference is change then notifyDataSetChanged() does't work .Whenever You are deleting elements from list view also delete them from the list you are using whether it is a ArrayList<> or Something else then Call notifyDataSetChanged() on object of Your adapter class.

So here see how i managed it in my adapter see below

public class CountryCodeListAdapter extends BaseAdapter implements OnItemClickListener{

private Context context;
private ArrayList<CountryDataObject> dObj;
private ViewHolder holder;
private Typeface itemFont;
private int selectedPosition=-1;
private ArrayList<CountryDataObject> completeList;

public CountryCodeListAdapter(Context context, ArrayList<CountryDataObject> dObj) {
    this.context = context;
    completeList=new  ArrayList<CountryDataObject>();
    itemFont=Typeface.createFromAsset(context.getAssets(), "CaviarDreams.ttf");

public int getCount() {
    return dObj.size();

public Object getItem(int position) {
    return dObj.get(position);

public long getItemId(int position) {
    return position;
public View getView(int position, View view, ViewGroup parent) {
        holder = new ViewHolder();
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.states_inflator_layout, null);
        holder.textView = ((TextView)view.findViewById(;
        holder = (ViewHolder) view.getTag();

    return view;
private class ViewHolder{
    private TextView textView;
    private ImageView checkImg;

public void getFilter(String name) {
    for (CountryDataObject item : completeList) {
    else {

public void onItemClick(AdapterView<?> parent, View view, int position,
        long id) {
    Registration reg=(Registration)context;

Solution 13

Consider you have passed a list to your adapter.


to update your list.

Solution 14

After deleting data from list view, you have to call refreshDrawableState(). Here is the example:

final DatabaseHelper db = new DatabaseHelper (ActivityName.this);;




and deleteContact method in DatabaseHelper class will be somewhat looks like

public boolean deleteContact(long rowId) {

   return db.delete(TABLE_NAME, BaseColumns._ID + "=" + rowId, null) > 0;


Solution 15

I was not able to get notifyDataSetChanged() to work on updating my SimpleAdapter, so instead I tried first removing all views that were attached to the parent layout using removeAllViews(), then adding the ListView, and that worked, allowing me to update the UI:

LinearLayout results = (LinearLayout)findViewById(;
ListView lv = new ListView(this);
ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
SimpleAdapter adapter = new SimpleAdapter( this, list, R.layout.directory_row, 
                new String[] { "name", "dept" }, new int[] {, } );

for (...) { 
    HashMap<String, String> map = new HashMap<String, String>();
    map.put("name", name);
    map.put("dept", dept);


Solution 16

while using SimpleCursorAdapter can call changeCursor(newCursor) on the adapter.

Solution 17

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:

public class Fragment01 extends implements ...
    private ListView                listView;
    private ArrayAdapter<String>    arrayAdapter_string;


public void onActivityCreated(Bundle savedInstanceState)
    this.listView= (ListView) super.getActivity().findViewById(;
    this.arrayAdapter_string= new ArrayAdapter<String>(super.getActivity(), R.layout.dispositivo_ble_item,;

public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord)
    super.getActivity().runOnUiThread(new RefreshListView(device));

private class RefreshListView implements Runnable
    private BluetoothDevice bluetoothDevice;

    public RefreshListView(BluetoothDevice bluetoothDevice)
        this.bluetoothDevice= bluetoothDevice;

    public void run()
        Fragment01.this.arrayAdapter_string.add(new String(bluetoothDevice.toString()));

Then the ListView began to dynamically populate with the mac address of the devices found.

Solution 18

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.

Solution 19

I had an ArrayList which I wanted to display in a listview. ArrayList contained elements from mysql. I overrided onRefresh method and in that method I used tablelayout.removeAllViews(); and then repeated the process for getting data again from the database. But before that make sure to clear your ArrayList or whatever data structre or else new data will get appended to the old one..

Solution 20

If you want to update the UI listview from a service, then make the adapter static in your Main activity and do this:

public void onDestroy() {
    if (MainActivity.isInFront == true) {
        if (MainActivity.adapter != null) {


Solution 21

If you are going by android guide lines and you are using the ContentProviders to get data from Database and you are displaying it in the ListView using the CursorLoader and CursorAdapters ,then you all changes to the related data will automatically be reflected in the ListView.

Your getContext().getContentResolver().notifyChange(uri, null); on the cursor in the ContentProvider will be enough to reflect the changes .No need for the extra work around.

But when you are not using these all then you need to tell the adapter when the dataset is changing. Also you need to re-populate / reload your dataset (say list) and then you need to call notifyDataSetChanged() on the adapter.

notifyDataSetChanged()wont work if there is no the changes in the datset. Here is the comment above the method in docs-

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

Solution 22

I was only able to get notifyDataSetChanged only by getting new adapter data, then resetting the adapter for the list view, then making the call like so:

    expandableAdapter = baseFragmentParent.setupEXLVAdapter();

Solution 23

on other option is onWindowFocusChanged method, but sure its sensitive and needs some extra coding for whom is interested

 override fun onWindowFocusChanged(hasFocus: Boolean) {

       // some controls needed
        programList = usersDBHelper.readProgram(model.title!!)
        notesAdapter = DailyAdapter(this, programList)
        listview_act_daily.adapter = notesAdapter

Solution 24

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, call clear()).

To you to get some idea, what was my code look like,

public class WordAdapter extends ArrayAdapter<Word> {
  Context context;

  public WordAdapter(Activity context, ArrayList<Word> words) {}

    public View getView(final int position, View convertView, ViewGroup group) {
     ImageButton deleteBt = listItemView.findViewById(;
     deleteBt.setOnClickListener(new View.OnClickListener() {
         public void onClick(View v) {
             if (vocabDb.deleteWord( {
             } else{
             remove(getItem(position)); // <---- here is the trick ---<
             //clear() // if you want to clear everything

Note: here remove() and getItem() methods are inherit from the Adapter class.

  • remove() - to remove the specific item that is clicked
  • getItem(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,

    ArrayList<Word> wordList = new ArrayList();
    WordAdapter adapter = new WordAdapter(this, wordList);

    ListView list_view = (ListView) findViewById(;

Solution 25

The easiest is to just make a new Adaper and drop the old one:

myListView.setAdapter(new MyListAdapter(...));