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?
-
can you show a piece of your code– AbhishekApr 15, 2015 at 13:06
-
use adapter.notifyDataSetChanged(); method of adapter– Bhavdip SagarApr 15, 2015 at 13:09
-
can you post some code– UmairApr 15, 2015 at 13:11
3 Answers
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);
-
after adding new data to list(data) you can also call adapter.notifyDataSetChanged();– niteshApr 15, 2015 at 13:30
You can re-query the database to get the latest data and then repopulate your listview's adapter.
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();
}
}