简体   繁体   English

Android-填充列表视图适配器的asynctask

[英]Android - asynctask that fills an adapter for a listview

Basically the flow is like the following: when a fragment is created (or when the user swipes to refresh the layout), an AsyncTask is executed. 基本上,流程类似于以下内容:创建片段(或用户滑动以刷新布局)时,将执行AsyncTask。 The ASyncTask retrieves the info from a URL and setup an adapter which is then assigned to a ListView. ASyncTask从URL检索信息并设置适配器,然后将其分配给ListView。

This is the code of the Fragment: 这是片段的代码:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                     Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.tasks_page, container, false);
    mSwipeRefreshLayout = (SwipeRefreshLayout)
    rootView.findViewById(R.id.list_container);
    mSwipeRefreshLayout.setOnRefreshListener(this);
    mSwipeRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_bright,    
    android.R.color.holo_green_light, android.R.color.holo_red_light);

    lv = (ListView) rootView.findViewById(android.R.id.list);
    getTasks();

    return rootView;
}

private void getTasks() {
    new TasksRetriever(mSwipeRefreshLayout,tasksAdapter,lv).execute();
}

@Override
public void onRefresh() {
    mSwipeRefreshLayout.setRefreshing(true);
    new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    getTasks();
                }
            }, 1000);
}

The TaskRetriever constructor is: TaskRetriever构造函数是:

public TasksRetriever(SwipeRefreshLayout srl, TasksAdapter ta, ListView lv) {
    adpt = ta;
    this.srl = srl;
    this.lv = lv;
}

and postExecute is: 和postExecute是:

@Override
protected void onPostExecute(List<TasksItem> result) {
    super.onPostExecute(result);
    dialog.dismiss();
    if (adpt == null) adpt = new TasksAdapter(result);
    else adpt.setItemList(result);
    lv.setAdapter(adpt);
    adpt.notifyDataSetChanged();
    srl.setRefreshing(false);
}

Not sure yet if it works but I was wondering if I'm on the right track because it doesn't look clean to me. 尚不确定它是否有效,但我想知道我是否走对了,因为它看起来不干净。 On the other hand, I can't create the adapter and assign it to the ListView until I actually have data for it... Is this considering OK or am I doing it wrong? 另一方面,在我实际有适配器的数据之前,我无法创建适配器并将其分配给ListView……这是否考虑得好还是我做错了?

I would set the list adapter up in the onCreate() method under your list view and make this a instance variable (not a local one) so that the Async task can access it as follows ie 我将在列表视图下的onCreate()方法中设置列表适配器,并将其设置为实例变量(而不是本地变量),以便Async任务可以按以下方式访问它:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                     Bundle savedInstanceState) {

    lv = (ListView) rootView.findViewById(android.R.id.list);
    mAdpt = new TasksAdapter(null);
    lv.setAdapter(mAdpt);

}

And then in your onPostExecute() 然后在你的onPostExecute()

@Override
protected void onPostExecute(List<TasksItem> result) {
    dialog.dismiss();  
    mAdpt.clear();
    for( TaskItem ti : result ) 
    {
      mAdpt.add( ti );
    }
    mAdpt.notifyDataSetChanged();
    srl.setRefreshing(false);
}

Also make sure you account for the null value being passed into your adapter in the getView() method. 另外,请确保您在getView()方法中说明传递给适配器的null值。

Hope this helps. 希望这可以帮助。

you CAN create an empty adapter and fill it later - in AsyncTask 您可以创建一个空适配器并稍后填充它-在AsyncTask中

your onPostExecute() should look like: 您的onPostExecute()应该如下所示:

@Override
protected void onPostExecute(List<TasksItem> result) {
    dialog.dismiss();
    if (adpt == null){
      adpt = new TasksAdapter(result);
      lv.setAdapter(adpt);
    }else{
      adpt.clear();
    }
    for( TaskItem ti : result ) adpt.add( ti );
    adpt.notifyDataSetChanged();
    srl.setRefreshing(false);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM