Updating a ListFragment based on Async Task result

孤街醉人 提交于 2020-01-06 12:34:13

问题


Scenario : I am having a list fragment which displays a list of friends. When an list item is pressed, I'm showing the details of the friend in a dialog fragment.

Problem: In the dialog fragment, I'm having an option to delete the friend from my friend list (in a remote server) . I'm using Async tack to perform this. After returning from the dialog fragment, how should I update the 'old' friend list. How do I trigger a fragment 'reload'. Since the async task is not aware of the custom list adapter being used in the list fragment.

Sorry, I will add the code ASAP. If anyone have met this scenario before kindly provide your suggestions.

Edit: After successfully deleting a friend from the friend list in a remote server, I want to reload the list being displayed in the list fragment. Is there a way to trigger the dataSetChanged notification in the list fragment itself.


回答1:


No problem. I'm guessing the ListFragment is reading from some data source - be it an ArrayAdapter or a SimpleCursorAdapter (or something similar) - and your task is also modifying that source. When the AsyncTask finishes, you should just be able to get that fragment and update the underlying data. For example:

// ...
void onPostExecute(Void result) {
    ListFragment lFrag = (ListFragment) getFragmentManager().findFragmentById(R.id.yourListFragment);
    BaseAdapter adapter = (BaseAdapter) lFrag.getListAdapter();
    adapter.notifyDataSetChanged();
}



回答2:


So you will be populating the ListView with an ArrayList.

  • Delete the Item from the ArrayList with index = ListViewItemIndex clicked.
  • call notifyDataSetChanged on the Adapter you are using.


来源:https://stackoverflow.com/questions/14152767/updating-a-listfragment-based-on-async-task-result

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