Android 片段列表未得到更新
我有一个Android 片段列表未得到更新,android,listview,fragment,Android,Listview,Fragment,我有一个FragmentActivity,在其中我启动了一个AsyncTask,它收集一个数据列表。这个活动有一个ListFragment,我想在数据不断到来时更新listview 我还有另一个片段也应该获得数据,所以我实现了一些observer模式的快速版本,这样当元素添加到列表中时,它们都会得到通知: private void addElement(MyListElement item) { myList.add(item); Collections.sort(myL
FragmentActivity
,在其中我启动了一个AsyncTask
,它收集一个数据列表。这个活动有一个ListFragment
,我想在数据不断到来时更新listview
我还有另一个片段也应该获得数据,所以我实现了一些observer模式的快速版本,这样当元素添加到列表中时,它们都会得到通知:
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();
}
}
在updateList()调用之后,列表适配器仍保留对myList原始值的引用。仅设置MyListFragment.myList不会更新适配器。您需要一种设置MyListAdapter的列表副本的方法。如果这是一个常用的Android适配器,您可能需要为新列表创建一个新适配器,并将其设置在列表片段上。发布R.layout.layout\u listview的内容;仅供参考,因为您使用的是ListFragment,该片段已经有listview和textView,如果您想要自定义布局,listview需要有一个特定的ID,如我之前的一篇文章中所述:post updated with layout
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]);
}
}
<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>