Hades10
Hades10

Reputation: 53

How to reload listview on a activity?

I have 2 activity. In activity 1, i have a listview show data from database and button Insert Activity 2 use to add something to database. When I click button ADD in activity 2, i call method finish() and return to activity 1. So, somebody can show me how to reload listview in activity 1 please?

Upvotes: 0

Views: 43

Answers (3)

khumche
khumche

Reputation: 82

Recreating the adapter should help, because you need to get a new dataset as Kaaaarll said.

So for example you have something like this in your code:

MyData data = database.getData();
MyAdapter adapter = new MyAdapter(data);
ListView list = new ListView();
list.setAdapter(adapter);

and in the next run, in the onResume(), get fresh data and reapply the adapter:

MyData data = database.getData();
MyAdapter adapter = new MyAdapter(data);
list.setAdapter(adapter);

Upvotes: 1

Karl-John Chow
Karl-John Chow

Reputation: 805

Notify the dataset in the onResume() method activity 1. It should be something like listViewAdapter.notifyDataSetChanged();. Don't forget to check whether they are null or not.

Let me know if it worked.

EDIT:

Something like this should do the work (in activity 1):

@Override
protected void onResume() {
    super.onResume();
    if (listViewAdapter != null) {
        listViewAdapter.notifyDataSetChanged();
    }
}

Upvotes: 0

Catalina
Catalina

Reputation: 2098

You can re-query the database to get the latest data and then repopulate your listview's adapter.

Upvotes: 0

Related Questions