Hajitsu
Hajitsu

Reputation: 784

Clear ListView Items and Refresh it in Android

I use internet service to fill a list view. I want to refresh the list view items when user press the refresh button. how can i do this?

In AsyncTask and postExecute i fill the list view:

protected void onPostExecute(String file_url) {
    pDialog.dismiss();
    try {
        if (jSon.has(KEY_SUCCESS)) {
            String success = jSon.getString(KEY_SUCCESS);
            if (success.equals("1")) {

                notes = jSon.getJSONObject("notes");
                for (int i = 0; i < notes.length(); i++) {
                    JSONObject c = notes.getJSONObject(Integer
                            .toString(i));

                    Log.i("JSONObject c >>", c.toString());

                    String id = c.getString(KEY_NOTE_ID);
                    String subject = c.getString(KEY_NOTE_SUBJECT);
                    String date = c.getString(KEY_NOTE_DATE);

                    HashMap<String, String> map = new HashMap<String, String>();
                    map.put(KEY_NOTE_ID, id);
                    map.put(KEY_NOTE_SUBJECT, subject);
                    map.put(KEY_NOTE_DATE, date);

                    noteList.add(map);
                }

            }
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }

    runOnUiThread(new Runnable() {

        public void run() {
            ListAdapter adapter = new SimpleAdapter(AllNotes.this,
                    noteList, R.layout.list_item, new String[] {
                            KEY_NOTE_ID, KEY_NOTE_SUBJECT,
                            KEY_NOTE_DATE }, new int[] {
                            R.id.list_lbl_id, R.id.list_lbl_subject,
                            R.id.list_lbl_date });
            setListAdapter(adapter);
        }
    });
}

Upvotes: 0

Views: 838

Answers (1)

Sam
Sam

Reputation: 86958

The most basic approach is to empty noteList and run your AsyncTask again.

  1. Inside onPreExecute() (or the first line of doInBackground()) call either:

    noteList = new ArrayList<Map<String, String>>(); // or noteList.clear();
    
  2. Call your AsyncTask's execute() method again.

Also I don't believe you need to use runOnUiThread() in onPostExecute() because it already has access to the main thread.

Upvotes: 1

Related Questions