清除ListView项目并在Android中刷新它

时间:2012-11-13 19:15:19

标签: android listview

我使用互联网服务填写列表视图。我想在用户按下刷新按钮时刷新列表视图项。我怎么能这样做?

在AsyncTask和postExecute中填写列表视图:

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

1 个答案:

答案 0 :(得分:1)

最基本的方法是清空noteList并再次运行AsyncTask。

  1. onPreExecute()内(或doInBackground()的第一行)调用:

    noteList = new ArrayList<Map<String, String>>(); // or noteList.clear();
    
  2. 再次调用AsyncTask的execute()方法。

  3. 此外,我不认为您需要在runOnUiThread()中使用onPostExecute(),因为它已经可以访问主线程。

相关问题