0

Hi guys is it possible that everytime I add an item in the database it will notify and update the listview? For example I have two fragments in one activity then When I send a data in the database from the Fragment A the Listview in Fragment B will update also without using interface.

Mark Angelo Bajaro
  • 103
  • 1
  • 3
  • 6
  • Possible duplicate of [how to refresh the listView using the Cursor Adapter](http://stackoverflow.com/questions/20676701/how-to-refresh-the-listview-using-the-cursor-adapter) – Rakesh Feb 12 '16 at 03:07

1 Answers1

0

There is a function of ArrayAdapter called - notifyDataSetChanged()

   //getting the list view 
    ListView userListView = (ListView) findViewById(R.id.users_ListView);

    //creating an array to represnt what ever you want
    ArrayList<String> users = new ArrayList<>();

    for (int i = 0; i < 10 ; i++) {

        //populate the array
        String s = "user " + i;
        users.add(s);

    }


    //setting up adapter to the array (this is 1 row with 3 arguments)
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(),
                                   android.R.layout.simple_list_item_1,
                                   users);

    //connecting the list view to get the data to show from the adapter
    userListView.setAdapter(adapter);

    //changing the arrayList by adding or subtracting  
    String s = "another user";
    users.add(s);

    //update the adapter and list view with this command
    adapter.notifyDataSetChanged();

Share your code if you are having any trouble.

good luck =)

Roee
  • 1,155
  • 3
  • 15
  • 24