2013-03-07 168 views
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]); 
    } 

} 

有趣的是,如果我把這個AsyncTaskListFragment,並從那裏調用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> 
+0

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

+0

發佈更新與佈局 – 2013-03-07 13:57:23

回答

0

的updateList()調用後,您的列表適配器仍持有到myList中的原始值的參考。簡單地設置MyListFragment.myList將不會更新適配器。您需要一種設置MyListAdapter列表副本的方法。如果這是Android適配器的庫存,您可能需要爲新列表創建新的適配器並將其設置在列表片段上。

+0

你是對的。這是問題所在。謝謝! – 2013-03-07 15:15:19