我有一个FragmentActivity
其中我发射AsyncTask
收集数据列表。此活动有ListFragment
,我想在数据不断更新时更新列表视图。片段列表没有得到更新
我还有一个片段也应该得到的数据,所以我实现观察者模式的一些快速的版本,让他们既当一个元素被添加到列表中得到通知:
private void addElement(MyListElement item) {
myList.add(item);
Collections.sort(myList, Collections.reverseOrder());
notifyObservers();
}
private void notifyObservers() {
for(IListObserver dObserver: observers){
dObserver.updateList(myList);
}
}
但我列表永远不会更新(或者其他片段)。
这里是ListFragment代码:
public class MyListFragment extends SherlockListFragment implements IListObserver{
private TextView first;
private Context mContext;
private List<MyListElement> myList;
private MyListAdapter myListAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
this.mContext = getActivity().getApplicationContext();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.layout_listview, container, false);
myList = new ArrayList<SizeDirectory>(0);
myListAdapter = new MyListAdapter(getActivity(), myList);
setListAdapter(myListAdapter);
myListAdapter.notifyDataSetChanged();
return view;
}
@Override
public void updateList(List<MyListElement> myList) {
this.myList = myList;
this.myListAdapter.notifyDataSetChanged();
getListView().requestLayout();
}
}
这是的AsyncTask:
class ListLoader extends AsyncTask<String, MyListElement, Boolean> {
private String LOG_TAG = ListLoader .class.getSimpleName();
/**
* Maybe show loading indicator
*/
@Override
protected void onPreExecute() {
super.onPreExecute();
}
protected Boolean doInBackground(String... args) {
MyListElement item = getListItem(...)
publishProgress(item);
return true;
}
/**
* After completing background task Dismiss the progress dialog
**/
protected void onPostExecute(Boolean result) {
if (result) {
//Everything was ok
}
}
@Override
protected void onProgressUpdate(MyListElement... values) {
super.onProgressUpdate(values);
addElement(values[0]);
}
}
有趣的是,如果我把这个AsyncTask
我ListFragment
,并从那里调用updateList
,那么随着asynctask的进展,我会得到更新的列表。
虽然也许这是由于生命周期调用创建活动(并因此重置列表)后我的片段的onCreateView,但我检查与调试器,情况并非如此。我也假设这与无法从另一个线程更新UI无关,因为我从onProgressUpdate执行它,再加上我会得到一个异常(或不是我?)。
编辑:layout_listview
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/firstTextView"
android:layout_width="fill_parent"
android:layout_height="129dp"
android:scrollbars="vertical"
android:text="@string/hello_world" />
<Button
android:id="@+itd/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Button" />
</RelativeLayout>
<ListView
android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
R.layout.layout_listview;仅供参考,因为您正在使用ListFragment,该片段已经有一个listview和一个textView,如果你想要一个自定义布局,listView需要有一个特定的ID,如我之前的帖子所述:http://stackoverflow.com/questions/15113969/onclicklistner-not-working-in-fragment-listview/15223134#15223134 – Atrix1987 2013-03-07 13:56:13
发布更新与布局 – 2013-03-07 13:57:23