How to update ListView after onResume from other activity?

百般思念 提交于 2019-12-01 11:16:33

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();

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!