Diaa
Diaa

Reputation: 879

Android: Loading a listview from a database after populating it asynchronously

I'm trying to show a progress dialog while loading information from the web that populates an sqlite database from which a Listview is created. I created an async class that displays a progress dialog box while the information is downloaded but after that the listview is empty. That is probably because the listview is already loaded before async. So how do I reload the listview or do this the right way? Here is what I have so far (left out the background processing since it's too long to displag):

private class DownloadInfo extends AsyncTask <Void, Void, Void>{
    //////////////////Open Dialog before execution ///////////
private ProgressDialog dialog;
    protected void onPreExecute(){
        dialog = new ProgressDialog(CandidatesList.this);
        dialog.setMessage("Loading Candidates from Webserver");
        dialog.setIndeterminate(true);
        dialog.setCancelable(false);
        dialog.show();
    }
    ///////////////Background Processing//////////////////
    @Override
    protected Void doInBackground(Void... dbArray) {
    ////Load information from webserver and put it in sqlite///
        return null;

    }

    ///////After the processing is done //////////////
    protected void onPostExecute(Void unused) {
        dialog.dismiss();
    }

}

And the on create is as follows:

DownloadInfo task = new DownloadInfo();
    task.execute();
Cursor c = db.getAllCandidates();
    candidates = new String[c.getCount()];
    if (c.moveToFirst()) {
        int i = 0;
        do {
            candidates[i] = c.getString(1);// insert Name into candidate
                                            // Array
            i++;
        } while (c.moveToNext());
    }
    setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item,
            candidates));

Please help, Thank you

Upvotes: 0

Views: 837

Answers (1)

Sam
Sam

Reputation: 86948

Use a SimpleCursorAdapter for your ListView if you want to load the cursor information from your database automatically into a ListView. (You may need to call notifyDataSetChanged() after the Async finishes.)

Upvotes: 1

Related Questions