简体   繁体   English

从AsyncTask更新ListView

[英]updating ListView from AsyncTask

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. 我有两个类HomeActivity和CustomListAdapter.HomeActivity类扩展了一个活动,然后更新了一个listview。我正在使用一个扩展BaseAsapter的CustomListAdapter类将数据填充到listview中。一切工作正常,但我想将数据加载到一个后台任务。当我这样做时,出现错误。 Here is my implementation of the onPostExecute of the AyncTask class. 这是AyncTask类的onPostExecute的实现。

@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));
            }
        });

    }

I get an error telling me that i should change the constructor on CustomListAdapter.But when i change it,everything goes downhill. 我收到一条错误消息,告诉我应该更改CustomListAdapter上的构造函数。但是当我更改它时,一切都会变得艰难。 I have tried this unsuccessfully too 我也尝试了失败

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

shares is an arraylist of data from the web service. 共享是来自Web服务的数据的数组列表。 Here is the constructor in the CustomListAdapter class 这是CustomListAdapter类中的构造函数

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

How can go about it?Help will be highly appreciated. 怎么办?我们将不胜感激。

You have to change : 您必须更改:

 @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));
        }
    });

}

See the below code works for me 看到下面的代码对我有用

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);

        progressDilaog.dismiss();

        itemListAdapter = new ItemListBaseAdapter(
                        IncidentListActivity.this, Util.arrIncidents);
        gridView.setAdapter(itemListAdapter);
     }

And one more thing, you don't require runOnUiThread in onPostExecute method. 还有一件事,您在onPostExecute方法中不需要 runOnUiThread You can directly change your listview. 您可以直接更改列表视图。

You are sending your AsyncTask context to your Adapter , you should put your Activity context which hosts your Listview to your AsyncTask class then use that Context to construct your Adapter. 您正在将AsyncTask上下文发送到Adapter,应将承载Listview的Activity上下文放到AsyncTask类,然后使用该上下文构造您的Adapter。

Edit : look at this as an example and onPostExecute by default runs on Ui thread and it doesn't need to define a runOnUiThread 编辑 :以示例为例,默认情况下onPostExecute在Ui线程上运行,并且不需要定义runOnUiThread

public class SyncHandler extends AsyncTask<String, Void, ResponseType>{
private Activity activity;
public SyncHandler (Activity activity)
{
    this.activity = activity;
}

then in your calling Activity pass the Activity context to your Async class : 然后在您的调用Activity中将Activity上下文传递给您的Async类:

new SyncHandler(this).execute();

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

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