Atarang
Atarang

Reputation: 422

android asynctask update to the listview in postexecute

Hi I'm having problem refreshing my listview after Async operation.
I have a simplecursoradapter, and custon listview and a button. Initially when application starts, it sets the listview from the data read from database. Then when user clicks a button, it starts a async code to download some data which gets inserted into a database. When async task start, I'm displaying a progressdialog, which I dismiss in postexecute(). Data is getting downloaded fine, but now how do I requery the cursor and update listview on the main thread after background job is done?

A Method "refreshRemoteData" gets called via a menu button.

This is how my AsyncTask looks like.

public class MyActivity extends ListActivity {

  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
  }

  public void onStart() {
    myDBAdapter = new DBAdapter(this);
myDBAdapter.open();
    populateMyList();
  }

  private void populateMyList() {
    myCursor = myDBAdapter.fetchAllItems();
    startManagingCursor(myCursor); 

    getListView().setAdapter(myDBAdapter);        
  }

  private void refreshRemoteData() {
    mPleaseWaitDialog = ProgressDialog.show(ExpirationDateTrackingActivity.this,
    "Data", "Downloading data", true, true);

download_task = new InfoDownloaderTask();
download_task.execute();
 }

  private class InfoDownloaderTask extends AsyncTask<Object, String, Boolean> {
private static final String DEBUG_TAG = "InfoDownloaderTask";

    protected DBAdapter mylocalDBAdapter=null; 

    @Override
    protected void onPreExecute() {
  Log.e(DEBUG_TAG, "onPreExecute: ");
      mylocalDBAdapter = new DBAdapter(this);
  mylocalDBAdapter.open();
    }

    @Override
    protected void onPostExecute(Boolean result) {
      Log.i(DEBUG_TAG, "onPostExecute: " ); 
      mPleaseWaitDialog.dismiss();
      mlocalDBAdapter.close();
    }

    @Override
     protected Boolean doInBackground(Object... arg0) {
        Log.v(DEBUG_TAG, "doInBackground");
        ///...
        //Update the database 
        mylocalDBAdapter.insertData(....);
        return true;
    }
 } //AsyncTask
}

I don't see my listview getting updated with new list data right after async operation is complete. But If I invoke another ativity and comeback to the listview then I see all new items (list update).

What am I missing?

Upvotes: 1

Views: 3525

Answers (1)

Ted Hopp
Ted Hopp

Reputation: 234795

You're inserting data through mylocalDBAdapter, but you aren't telling myDBAdapter about it. Try calling myDBAdapter.notifyDataSetChanged(); at the end of onPostExecute().

Upvotes: 1

Related Questions