Skip to content Skip to sidebar Skip to footer

How To Update Listview After Onresume From Other Activity?

Im trying to create an application that enable user to create event, then invite participant. So when user go to 'add participant' page, after entering all information, im trying t

Solution 1:

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 :

@OverrideprotectedvoidonResume()
{
    super.onResume();

    if (getListView() != null)
    {
        updateData();
    }
}

privatevoidupdateData()
{
    SimpleAdapter adapter = newSimpleAdapter(EventPage.this, 
                                              controller.getAllFriends(queryValues), 
                                              R.layout.view_friend_entry, 
                                              newString[] {"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():

privatestaticfinalintADD_PARTICIPANT=123;// the unique code to start the add participant activity @OverrideprotectedvoidonActivityResult(int requestCode, int resultCode, Intent data)
{
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == ADD_PARTICIPANT && resultCode == Activity.RESULT_OK)
    {
        updateData();
    }
}

privatevoidupdateData()
{
    SimpleAdapteradapter=newSimpleAdapter(EventPage.this, 
                                              controller.getAllFriends(queryValues), 
                                              R.layout.view_friend_entry, 
                                              newString[] {"friendId", "friendName" },
                                              newint[] { 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();

Post a Comment for "How To Update Listview After Onresume From Other Activity?"