【问题标题】:Fragment list not getting update片段列表未更新
【发布时间】:2013-03-07 13:52:11
【问题描述】:

我有一个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,那么我确实会随着异步任务的进行而更新列表。

我虽然这可能是由于活动生命周期在创建活动(并因此重置列表)之后调用我的 Fragment 的 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,如我之前的一篇文章中所述:stackoverflow.com/questions/15113969/…
  • 帖子已更新布局

标签: android listview fragment


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多