1

請問我可以聽取建議。如何清除ListView,它只是在Android App中的舊內容之後添加新內容?

我想在Android中獲取內容到ListView中。

我傳入一個ArrayList類型的字符串,設置一個適配器傳入該內容,然後將適配器傳遞給listView。

問題出現在內容確實顯示的事實中,但是當我每次運行該活動時,新內容都只在舊內容的頂部列出。例如

活動名稱

item 1 
item 2 
--------- 
item 1 //repeats again but with new content 
item 2 
item 3 

我曾嘗試:

list.setAdapter(null); 
arrayAdapter.clear(); 
arrayAdapter.notifyDataSetChanged();; 

這些都不似乎工作。強制關閉應用程序確實畫在正確的內容。

有任何問題,請提出。

public void fillListView(){ 


     arrayAdapter = new ArrayAdapter<String>(
       this, 
       android.R.layout.simple_list_item_1, 
       StartSearch.myList22);    //fill adapter with content 


     lv = (ListView)findViewById(R.id.listView2); 

     lv.setAdapter(arrayAdapter); //feed data to listview 
    } 

此方法在每次打開活動時都會運行以刷新內容。

+0

你需要清除'StartSearch.myList22'數組 –

回答

3

讓您

StartSearch.myList22 as a Global variable and 

- >當你得到新的數據TR Ÿ以上列表類似下面的代碼答案: -

StartSearch.myList22.clear(); 
StartSearch.myList22.addall(NewList); 

- >如果它仍然does not工作嘗試下面的代碼: -

StartSearch.myList22.clear(); 
StartSearch.myList22.addall(NewList); 
arrayAdapter = new ArrayAdapter<String>(
      this, 
      android.R.layout.simple_list_item_1, 
      StartSearch.myList22); 
lv.setAdapter(arrayAdapter); 

-Thanks〜!

0

對不起,讓我更具體。 我覺得你不必設置適配器爲null,清除它

您可以更改設置適配器像

yourList.clear(); 
yourList.add(data); 

一旦設置的改變,你的數據數組列表然後調用:

yourAdapter.notifyDataSetChanged(); 
+0

嗨對不起,嘗試這個沒有運氣更新的主要帖子。 – Rueben

相關問題