2014-05-02 104 views
1

我已經檢查了關於ListFragments的異步更新的其他答案,並發現notifyDataSetChanged()方法無法正常工作的最常見問題是,開發人員傾向於覆蓋初始適配器源,導致適配器丟失參考,因此不更新視圖。我有一個ListFragment,我想異步更新。我決定要做的是解析列表數據AsyncTask,最後更新片段onPostExecute()。我希望界面儘可能流暢,所以我想避免在主線程上進行繁重的處理。在創建視圖之後,我會撥打AsyncTask以避免null指針。異步更新ListFragment

public class CategoryFragment extends ListFragment { 

ArrayList<Category> categoriesArray = new ArrayList<Category>(); 
CategoryAdapter adapter; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    View rootView = inflater.inflate(R.layout.fragment_category, container, false); 
      adapter = new CategoryAdapter(getActivity(), R.layout.category_list_item, categoriesArray); 
      setListAdapter(adapter); 
    return rootView; 
} 

@Override 
public void onViewCreated(View view, Bundle savedInstanceState) { 
    new UpdateUITask().execute(categories); 
} 

... 

    // The async task to update the UI. 
class UpdateUITask extends AsyncTask<String, String, ArrayList<Category>>{ 

    @Override 
    protected ArrayList<Category> doInBackground(String... input) { 

     // Do some data processing, to fill the categoriesArray. 
     // categoriesArray.add(...); -- loop 
     return categoriesArray; 
    } 

    @Override 
    protected void onPostExecute(ArrayList<Category> result) { 
     super.onPostExecute(result); 
     adapter.notifyDataSetChanged(); 
    } 
} 
} 

刷新方法火災,但不產生任何效果。我還有什麼在這裏錯過?我應該選擇一種完全不同的方法嗎?

+0

您正在使用哪種類型的適配器,它是ArrayAdapter? – umesh

+0

這是一個自定義適配器。如果相關,我會很高興發佈適配器的一些代碼。 –

+0

不只是確保您通過擴展ArrayAdapter來創建客戶適配器,否則您的notifyDataSerChanged將不起作用。 – umesh

回答

1

它看起來像你的類ArrayArray的實例正在迷路。 adapter.notifyDataSetChanged();僅在您剛剛傳遞給適配器的listArray引用丟失或更改時才起作用。所以,我會推薦你​​請確保這一點。

另外,如果您要填充自定義數組,請使用AsyncTaskonProgressUpdate() method。它也會減少加載時間。爲您的AsyncTask下面的代碼

class UpdateUITask extends AsyncTask<String, Category, ArrayList<Category>> 
    { 

     @Override 
     protected ArrayList<Category> doInBackground(String... input) 
     { 

      // Do some data processing, to fill the categoriesArray. 
        // and get the category objects one by one and call 
        //publishprogress till data is there 
      publishProgress(Category); 

        // and finallly just return somthing to get in onpostexecute 

     } 

     @Override 
     protected void onProgressUpdate(Category... values) 
     { 
      // TODO Auto-generated method stub 
      super.onProgressUpdate(values); 
        categoriesArray.add(...); 
        adapter.notifyDataSetChanged(); 
     } 

     @Override 
     protected void onPostExecute(ArrayList<Category> result) 
     { 
      super.onPostExecute(result); 
      adapter.notifyDataSetChanged(); 
     } 
    } 
0

使用,但請確保你又在原來的所屬分類變化: 你可以做到這一點是這樣的。

private class UpdateUITask extends AsyncTask<Void, Void, Void> { 

    @Override 
    protected Void doInBackground(Void... params) { 
     // Do some data processing, to fill the categoriesArray. 
     return null; 
    } 

    @Override 
    protected void onPostExecute(Void result) { 
     adapter.notifyDataSetChanged(); 
    } 
} 

並且只需通過傳遞任何參數來運行此任務。

new UpdateUITask().execute();