0

我有一个可以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>
4

1 回答 1

0

在 updateList() 调用之后,您的列表适配器仍然持有对 myList 原始值的引用。简单地设置 MyListFragment.myList 不会更新适配器。您需要一种设置 MyListAdapter 的列表副本的方法。如果这是现有的 Android 适配器之一,您可能需要为新列表创建一个新适配器并将其设置在列表片段上。

于 2013-03-07T15:11:29.000 回答