updating ListView from AsyncTask

倖福魔咒の 提交于 2019-12-08 03:52:58

问题


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

    }

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 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.


回答1:


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

}



回答2:


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. You can directly change your listview.




回答3:


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.

Edit : look at this as an example and onPostExecute by default runs on Ui thread and it doesn't need to define a 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 :

new SyncHandler(this).execute();


来源:https://stackoverflow.com/questions/17422705/updating-listview-from-asynctask

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!