2014-11-25 73 views
0

我在類中聲明瞭ArrayList並在啓動Async任務時啓動它;然後我在doInBackGround()方法中添加項目,但在此之後,當我將數組列表發送到適配器的構造函數時,數組列表的大小仍爲0.添加項目後ArrayList的大小爲0

我真的不知道我是什麼做錯了。請幫助。

這裏是我的代碼:

public class FindNewThingFragment extends android.support.v4.app.Fragment { 
    private PullRefreshLayout pullRefreshLayout; 
    private ProgressBar progressBar; 
    private ListView listView; 
    private ArrayList<ThingsItem> arrayList; 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View view=inflater.inflate(R.layout.fragment_found_new_thing,container,false); 
    listView=(ListView)view.findViewById(R.id.things_item_list_view); 
    progressBar=(ProgressBar)view.findViewById(R.id.things_item_progress_bar); 
    new LoadDongxiData().execute(); 
    ThingsItemListViewAdapter adapter=new ThingsItemListViewAdapter(getActivity(),arrayList); 
    listView.setAdapter(adapter); 

    return view; 
} 


@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setHasOptionsMenu(true); 
} 

private class LoadDongxiData extends AsyncTask<Void,Void,Void> { 
    public LoadDongxiData(){ 
     arrayList=new ArrayList<ThingsItem>(); 
    } 

    @Override 
    protected Void doInBackground(Void... voids) { 
      doSomething(); 
      for(int i=0;i<10;i++){} 
       arrayList.add(new ThingsItem(itemImageUrl, itemName, price, userIconUrl, userName, userComment, numberOfLikes, numberOfAddDouToList, numberOfReply, userHomePageLink, itemDetailLink)); 
      } 
     } 

    } 
} 


} 
+0

嘗試添加項列出onPostExecute,還可以設置適配器那裏。 – 2014-11-25 04:03:02

+1

您確定在*之後將其設置爲適配器嗎?後臺任務可能尚未完成。 – Thilo 2014-11-25 04:03:11

+0

@Thilo只需在'onPostExecute()'中調用'notifyDataSetChanged()'? :) – 2014-11-25 04:12:48

回答

0

更改代碼一樣,

public class FindNewThingFragment extends android.support.v4.app.Fragment { 
    private PullRefreshLayout pullRefreshLayout; 
    private ProgressBar progressBar; 
    private ListView listView; 
    private ArrayList<ThingsItem> arrayList; 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View view=inflater.inflate(R.layout.fragment_found_new_thing,container,false); 
    listView=(ListView)view.findViewById(R.id.things_item_list_view); 
    progressBar=(ProgressBar)view.findViewById(R.id.things_item_progress_bar); 
    new LoadDongxiData().execute(); 

    return view; 
} 


@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setHasOptionsMenu(true); 
} 

private class LoadDongxiData extends AsyncTask<Void,Void,Void> { 
    public LoadDongxiData(){ 
     arrayList=new ArrayList<ThingsItem>(); 
    } 

    @Override 
    protected Void doInBackground(Void... voids) { 
      doSomething(); 
      for(int i=0;i<10;i++){ 
       arrayList.add(new ThingsItem(itemImageUrl, itemName, price, userIconUrl, userName, userComment, numberOfLikes, numberOfAddDouToList, numberOfReply, userHomePageLink, itemDetailLink)); 

      } 

      } 

protected void onPostExecute(Long result) { 
if(arrayList != null && arrayList.size() > 0){ 
      ThingsItemListViewAdapter adapter=new ThingsItemListViewAdapter(getActivity(),arrayList); 
    listView.setAdapter(adapter); 
} 
    } 

     } 

    } 
} 


} 
+0

也可能使'arrayList'成爲該任務的私有字段。 – Thilo 2014-11-25 04:06:06

0
for(int i=0;i<10;i++){} 

這是空循環故意或這是你在哪裏嘗試添加的項目嗎?從你的括號縮進的方式來看,它似乎不是故意的。如果這意味着時間延遲,從1到10的計數將會少於幾分之一秒。

設置完數據後,撥打onPostExecute()即可撥打adapter.notifyDataSetChanged()

您可以在onCreate()中設置您的適配器。每當適配器的底層數據發生變化時,請不要忘記撥打adapter.notifyDataSetChanged()。不過,您需要將adapter設置爲「全局」變量。

0

設置你的適配器在Asynctask

0

注意,您的AsyncTask將在後臺運行的onPostExecute。而它在後臺運行的UI線程將繼續到你的代碼的下一行意味着之前

new LoadDongxiData().execute(); 

這行完成它的執行下一行將被調用。因此數組列表大小仍然爲0.

解決方案是您應該在LoadDongxiData()完成它的執行後有這樣的行。

ThingsItemListViewAdapter adapter=new ThingsItemListViewAdapter(getActivity(),arrayList); 
listView.setAdapter(adapter); 

LoadDongxiData的onPostExecute()方法後調用這些行。

0

嘗試移動添加項目列出並設置適配器代碼onPostExecute:

@Override 
protected void onPostExecute(Void aVoid) { 
    super.onPostExecute(aVoid); 
    for(int i=0;i<10;i++) { 
     arrayList.add(new ThingsItem(itemImageUrl, itemName, price, userIconUrl, userName, userComment, numberOfLikes, numberOfAddDouToList, numberOfReply, userHomePageLink, itemDetailLink)); 
    } 
    ThingsItemListViewAdapter adapter=new ThingsItemListViewAdapter(getActivity(),arrayList); 
    listView.setAdapter(adapter); 
} 
0

更新您的doInBackground()方法...

 protected Void doInBackground(Void... voids) { 
      doSomething(); 
      for(int i=0;i<10;i++){ 
       arrayList.add(new ThingsItem(itemImageUrl, itemName, price, userIconUrl, userName, userComment, numberOfLikes, numberOfAddDouToList, numberOfReply, userHomePageLink, itemDetailLink)); 

       } 
      } 
     }