清除Android ListView

时间:2014-04-09 06:39:20

标签: android listview android-listview

我有一个ListView。我想动态更改listview的内容。我使用" adapter.notifyDataSetChanged();"改变内容。我也尝试将listview的适配器设置为null但仍然得到相同的结果。它是令人耳目一新但不清除列表视图,而是将其与先前的结果一起附加。我想要的是应该删除以前的内容,并显示新的结果。

我无法理解我的错误在哪里以及我错过了什么。

如果有人能帮我解决这个问题,我会感激不尽。

以下是生成列表视图的代码

 public void ListDrawer() {
     try {
      JSONObject jsonResponse = new JSONObject(jsonResult);
      JSONArray jsonMainNode = jsonResponse.optJSONArray("myRegisteredList");

      for (int i = 0; i < jsonMainNode.length(); i++) {
       JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
       String registered_name = jsonChildNode.optString("seminar");
       String org = jsonChildNode.optString("organizer");
       String sem_id = jsonChildNode.optString("id");
       String r_sem = jsonChildNode.optString("seminar_id");

       registeredlist = new HashMap<String, String>();
       registeredlist.put("registeredList", registered_name);
       registeredlist.put("seminar_id", r_sem);
       registeredlist.put("sem_id", sem_id);
       registeredlist.put("organizer", org);
       registeredList.add(registeredlist);

      }
     } catch (JSONException e) {
         //Toast.makeText(this, e.getMessage().toString() + " 1", Toast.LENGTH_LONG).show();
         Toast.makeText(ViewReservedSeminarList.this, "Sorry...You don't have any reserved seminar/s.", Toast.LENGTH_LONG).show();
    }
     try{

    simpleAdapter = new SimpleAdapter(this, registeredList,
       android.R.layout.simple_list_item_2,
       new String[] { "registeredList" ,"organizer", "seminar_id" }, new int[] { android.R.id.text1 , android.R.id.text2 , android.R.id.title });


     listView.setAdapter(simpleAdapter);
     listView.setOnItemClickListener(new OnItemClickListener(){

        @Override
        public void onItemClick(AdapterView<?> a, View v, int i, long l) {
            // TODO Auto-generated method stub
            @SuppressWarnings("unchecked")
            HashMap<String, String> selected_row = (HashMap<String, String>) simpleAdapter.getItem(i);
            s_id = selected_row.get("seminar_id");
            Intent intent = new Intent(ViewReservedSeminarList.this, ReservedSeminar.class);
            startActivity(intent);
            //Toast.makeText(ViewReservedSeminarList.this, "Selected Item : "+s_id, Toast.LENGTH_LONG).show();
        }

     });
     }catch(Exception e){
         //Toast.makeText(this, e.getMessage().toString() + " 2", Toast.LENGTH_LONG).show();
        Toast.makeText(ViewReservedSeminarList.this, "There is no data being fetched", Toast.LENGTH_LONG).show();
 }
}

下面是我放在onCreate方法上的片段,每隔10秒刷新一次列表视图。我在accessWebService方法下调用ListDrawer方法。

 final Handler handler = new Handler();
                 handler.postDelayed( new Runnable(){
                        @Override
                        public void run(){
                        accessWebService();
                        handler.postDelayed(this, 10 * 1000);
                        Log.i("TAG", "notify");

                 }
                        }, 10 * 1000

                        ); 

4 个答案:

答案 0 :(得分:4)

请试试这个:

清除arraylist / HashMap

添加新项目

adapter.notifyDataSetChanged();

设置适配器。

答案 1 :(得分:2)

如果您使用ArrayAdapter代替SimpleAdapter,则可以致电

ArrayAdapter.clear();
ArrayAdapter.notifyDataSetChanged();

从适配器中删除所有元素。

答案 2 :(得分:0)

试试这个

final Handler handler = new Handler();
                 handler.postDelayed( new Runnable(){
                        @Override
                        public void run(){
                 //this line i have added in your code
                         registeredList.clear();
                        accessWebService();
                        handler.postDelayed(this, 10 * 1000);
                        Log.i("TAG", "notify");

                 }
                        }, 10 * 1000

                        ); 

答案 3 :(得分:0)

删除adapter.notifyDataSetChanged();希望它有效