简体   繁体   中英

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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