Created
December 14, 2015 07:43
-
-
Save salihyalcin/37d23955351f1782d8ac to your computer and use it in GitHub Desktop.
Refresh Adapter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
First, your adapter.notifyDataSetChanged() right before setListAdapter(adapter); in the onCreate() method is useless because the list isn't aware of the data unless you call setListAdapter(). | |
So here is the easy way in you class EventPage : | |
@Override | |
protected void onResume() | |
{ | |
super.onResume(); | |
if (getListView() != null) | |
{ | |
updateData(); | |
} | |
} | |
private void updateData() | |
{ | |
SimpleAdapter adapter = new SimpleAdapter(EventPage.this, | |
controller.getAllFriends(queryValues), | |
R.layout.view_friend_entry, | |
new String[] {"friendId", "friendName" }, | |
new int[] { R.id.friendId, R.id.friendName }); | |
setListAdapter(adapter); | |
} | |
So here, we basically refresh the data every time onResume() is called. | |
If you want to do it only after adding a new item, then you must use onActivityResult() instead on onResume(): | |
private static final int ADD_PARTICIPANT = 123;// the unique code to start the add participant activity | |
@Override | |
protected void onActivityResult(int requestCode, int resultCode, Intent data) | |
{ | |
super.onActivityResult(requestCode, resultCode, data); | |
if (requestCode == ADD_PARTICIPANT && resultCode == Activity.RESULT_OK) | |
{ | |
updateData(); | |
} | |
} | |
private void updateData() | |
{ | |
SimpleAdapter adapter = new SimpleAdapter(EventPage.this, | |
controller.getAllFriends(queryValues), | |
R.layout.view_friend_entry, | |
new String[] {"friendId", "friendName" }, | |
new int[] { R.id.friendId, R.id.friendName }); | |
setListAdapter(adapter); | |
} | |
Now, simply replace in your onCreate() method the startActivity by startActivityForResult(objIndent, ADD_PARTICIPANT); | |
Lastly, in your AddParticipant activity, add setResult(Activity.RESULT_OK); just before onFinish(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment