问题
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