我在類中聲明瞭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));
}
}
}
}
}
嘗試添加項列出onPostExecute,還可以設置適配器那裏。 – 2014-11-25 04:03:02
您確定在*之後將其設置爲適配器嗎?後臺任務可能尚未完成。 – Thilo 2014-11-25 04:03:11
@Thilo只需在'onPostExecute()'中調用'notifyDataSetChanged()'? :) – 2014-11-25 04:12:48