Archie.bpgc
Archie.bpgc

Reputation: 24012

Android: Changing the ListView data dynamically from other Activity

I have an Activity_A where there is a ListView whose data depends on a value say dependency

From this Activity_B user goes to Activity_B where he can change the value of dependency

since this dependency must change the data in the ListView i need to reload the ListView, hence i did this:

if(//dependency is changed){
Intent intent = new Intent(Activity_B.this,Activity_A.class);
startActivity(intent);
}

the ListView is populated with new data.

Problem:

when i press device back button from this newly loaded Activity_A twice, I end up in Activit_A with previous ListView data. So, if i try to click any item i get this Exception

The content of the adapter has changed but ListView  did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread

How to avoid this??

Thank You

Upvotes: 0

Views: 582

Answers (3)

NARESH REDDY
NARESH REDDY

Reputation: 682

In Activity A You have a ListView

in Activity A, onResume() method you set adapter for your ListView.

when you navigates to Activity_B through Intent and change the value of

dependency. on buton click don't start again Activity A just finish the

Activity_B. Then automatically onResume() method of Activity A will be called and

again listview is loaded. Hence changes effect to values of listview

note : Don't forget to declare dependency as static.

Upvotes: 0

Renjith
Renjith

Reputation: 5803

To go back to the previous activity you should not call new intent. Just call finish() on your ACtivity B. Then in your onResume() of Activity A, update the listview with the new values.

Here you are calling Activity A as a new Activity, and the previous Activity A is still in the backstack. So when you press back, you will reach the ACtivity A, which was your first Activity. But the values populating the listview has changed. So when you try to click on anything, exception occurs. Thats the problem.

So when you change the values to the listview, call finish() in Activity B. It will take you back to Activity A. In onResume() of Activity A, update the listview.

Upvotes: 1

Alexander
Alexander

Reputation: 48272

You can store dependency value in SharedPreferences and populate your list based on the dependency value in the Activity_A onResume()

Upvotes: 1

Related Questions