如何清空android ListView控制項的內容

來源:互聯網
上載者:User

標籤:android   color   io   ar   使用   sp   資料   on   問題   

============問題描述============


代碼如下:
BaseAdapter  ba=new BaseAdapter()
{

public int getCount() {
// TODO Auto-generated method stub
return count;
}

public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}

public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}

public View getView(int arg0, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
LinearLayout ll=new LinearLayout(ShowBusActivity.this);
ll.setOrientation(LinearLayout.HORIZONTAL);
TextView tv1=new TextView(ShowBusActivity.this);
tv1.setText(routeID);
tv1.setTextSize(12);
tv1.setWidth(120);
tv1.setTextColor(Color.WHITE);
ll.addView(tv1);
TextView tv2=new TextView(ShowBusActivity.this);
tv2.setText(tempBus[arg0].strCarId);
tv2.setTextSize(12);
tv2.setWidth(120);
tv2.setTextColor(Color.WHITE);
ll.addView(tv2);
TextView tv3=new TextView(ShowBusActivity.this);
tv3.setText(tempBus[arg0].distance);
tv3.setTextSize(12);
tv3.setWidth(120);
tv3.setTextColor(Color.WHITE);
ll.addView(tv3);
TextView tv4=new TextView(ShowBusActivity.this);
tv4.setText(tempBus[arg0].direction);
tv4.setTextSize(12);
tv4.setWidth(120);
tv4.setTextColor(Color.WHITE);
ll.addView(tv4);
return ll;
}};

lv.setAdapter(ba);
程式中tempBus中的內容是有使用者更新的,我想在每次更新時只顯示更新後的內容,可當程式運行時每更新一次就會在ListVeiw中再加一次更新後的內容,未更新前的內容也顯示在ListView 上。請高手們幫幫忙,要怎樣在更新資料前把ListView中的內容清空?

============解決方案1============


tempBus更新是吧!你把上一次顯的tempBus[]清空不就行了!你那裡面的TextView要清除輕型?

============解決方案2============


作者應該用適配器來添加ListView的Item比較好吧

給你提供一個思路:
1,當我們要載入item的時候,首先把 list清空

list.clear(); 

適配器

adapter1 =new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, list); 
listView.setAdapter(adapter1); 

這樣載入以後就把Listview 中的資料清空了

2,然後再載入我們想要的資料

list.add("a"); 
list.add("c"); 
list.add("b"); 
list.add("a");
list.add("c");
list.add("b");

適配器

adapter1 =new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, list); 
listView.setAdapter(adapter1); 
adapter1 =new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, list);
listView.setAdapter(adapter1);

這樣就可以清空以前的資料而得到我們更新的結果

如何清空android ListView控制項的內容

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.