我正在使用自定義適配器來顯示一個ListView。如何在Android中動態添加項目到ListView?
它工作正常。
但我需要將三個項目添加到ListView。我如何添加它們並讓它們顯示?我試過notifydatasetchanged()
方法不工作。
我正在使用自定義適配器來顯示一個ListView。如何在Android中動態添加項目到ListView?
它工作正常。
但我需要將三個項目添加到ListView。我如何添加它們並讓它們顯示?我試過notifydatasetchanged()
方法不工作。
您必須使用list.add(...)
將項目添加到列表中,然後通過adapter.notifyDataSetChanged()
告訴適配器。
+1也擊敗了我吧;) –
要將項目添加到列表視圖中,您必須使用List/ArrayList對象。使用此功能,您可以對列表視圖項目數據執行添加/編輯/刪除操作,並將此列表設置到您的適配器中,以從列表/數組列表中獲取數據並將其設置到列表項中。
好吧,現在假設我有ListView和要添加新的項目只需添加項目到列表/ ArrayList對象,並通知到適配器這樣
List<String> myList = new ArrayList<String>();
ArrayAdapter<String> adapter;
oncreate(){
// initi listview, adapter and set the myList object into adapter object and then set the adapter into listview
}
// on button click from somewhere
public void onClick(View view){
myList.add("hello friends");// you can add your data here into list object
adapter.notifyDataSetChanged();
}
把你的自定義適配器的代碼.. –
+1「在此先感謝「刪除;)提前沒有感謝。複選標記和正確答案表示感謝。 –
@BillMote:保持它.. –