从AsyncTask更新ListView [英] updating ListView from AsyncTask

查看:78
本文介绍了从AsyncTask更新ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个类,HomeActivity和CustomListAdapter.HomeActivity类扩展了一个活动,然后更新了一个列表视图.我正在使用一个扩展了BaseAsapter的CustomListAdapter类将数据填充到列表视图中.后台任务中的数据.当我这样做时,出现错误. 这是我对AyncTask类的onPostExecute的实现.

I have two classes,HomeActivity and CustomListAdapter.THe HomeActivity class extends an activity and then updates a listview.I am populating the data to the listview using a CustomListAdapter class which extends BaseAsapter.Everything is working fine but i want to load the data in a background task.When i do that,an error comes up. Here is my implementation of the onPostExecute of the AyncTask class.

@Override
    protected void onPostExecute(HomeActivity Params){
        progressDialog.dismiss();
        runOnUiThread(new Runnable(){
            public void run(){
                final ListView lv1 = (ListView) findViewById(R.id.listings);
                lv1.setAdapter(new CustomListAdapter(this, shares));
            }
        });

    }

我收到一条错误消息,告诉我应该更改CustomListAdapter上的构造函数.但是当我对其进行更改时,一切都会变得艰难. 我也尝试失败了

I get an error telling me that i should change the constructor on CustomListAdapter.But when i change it,everything goes downhill. I have tried this unsuccessfully too

final ListView lv1 = (ListView) findViewById(R.id.listings);
        lv1.setAdapter(new CustomListAdapter(this, shares)); 

shares是来自Web服务的数据的数组列表. 这是CustomListAdapter类中的构造函数

shares is an arraylist of data from the web service. Here is the constructor in the CustomListAdapter class

 public CustomListAdapter(Context context, ArrayList listData) {
    this.listData = listData;
    layoutInflater = LayoutInflater.from(context);
}

该怎么办?我们将不胜感激.

How can go about it?Help will be highly appreciated.

推荐答案

您必须进行以下更改:

 @Override
 protected void onPostExecute(HomeActivity Params){
    progressDialog.dismiss();
    runOnUiThread(new Runnable(){
        public void run(){
            final ListView lv1 = (ListView) findViewById(R.id.listings);
            lv1.setAdapter(new CustomListAdapter(YourActivity.this, shares));
        }
    });

}

这篇关于从AsyncTask更新ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆