Wyzard
Wyzard

Reputation: 60

Android refresh listview adapter from another activity

I Googled a lot, but can't find the proper answer.

I've got two activities. Each of that activities 'has' one listview.

Each of that listviews have a custom ArrayAdapter. So :

Activity 1 ----> Listview 1 ------> custom ArrayAdapter 1

Activity 2 ----> Listview 2 ------> custom ArrayAdapter 2

When I press a button in Activity 1, then it refreshes the Listview 1 (it's adapter).

The goal is : I have to do a refresh on Listview 2 too that time. How can I achieve this ? I've read something about broadcasting, but didn't understand it well.

Thank You !

Upvotes: 0

Views: 11415

Answers (2)

waqaslam
waqaslam

Reputation: 68177

If you are in Activity1, it's useless to try to refresh Activity2's listview as it's not visible. However, what you can do is, when the user is switched to Activity2, just call adapter's notifyDataSetChanged() in it's onResume() method and your Activity2 will be refreshed.

Upvotes: 12

dong221
dong221

Reputation: 3410

Add listView as global variable in each activity and make it public static.

Then, just call it when you need it.

e.g. in activity1, call the following to let the list in activity2 refresh

if(SecondActivity.listView != null)

    ((ArrayAdapter)SecondActivity.listView.getAdapter()).notifyDataSetInvalidated();

Upvotes: 1

Related Questions