[英]Adapter.NotifyDataSetChanged doesn't work
我的listView有適配器,並且我想使用NotifyDataSetChanged()更新適配器,但它對我不起作用。 這就是我在適配器中添加數據的方式:
if (Messages.Count != 0) {
ChatAdapter = new BubbleAdapter (this, Messages);
if(listViewChat.Adapter == null) {
listViewChat.Adapter = ChatAdapter;
} else {
ChatAdapter.NotifyDataSetChanged();
}
}
但是就像我說的那樣,它不起作用...如果我使用這種方式進行更新,我的listView會滾動到頂部:
ChatAdapter = new BubbleAdapter (this, Messages);
listViewChat.Adapter = ChatAdapter;
請看下面的代碼。 您必須執行與我在下面提到的類似的操作。 由於我沒有完整的代碼段,因此我修改了您提到的代碼:
ArrayList<MessageBubble> MessagesAll = new ArrayList<MessageBubble>();
ChatAdapter = new BubbleAdapter (this, MessagesAll);
listViewChat.setAdapter(ChatAdapter);
MessageBubble Messages = new MessageBubble();
Messages.setMessage("Hi");
if (Messages.Count != 0) {
MessagesAll.add(Messages);
ChatAdapter.notifyDataSetChanged();
}
請讓我知道您是否仍然沒有成功。
嘗試:
listViewChat.setAdapter(ChatAdapter);
您可以用它替換整個塊。 如果列表為空,則適配器也將為空,並且將以零項刷新listview。
使用ChatAdapter.notifyDataSetInvalidated(); 而不是ChatAdapter.notifyDataSetChanged();
首先,我要感謝堆棧的每個貢獻者。 我敢肯定,我欠你的感謝超過了我所能給予的。
這個問題占用了我太多時間。 對於仍在尋找解決方案的人們:
performFilter應該保持原樣。
publishResults應該很簡單:
mIngredientList =(ArrayList)results.values; adapter.clear(); if(mIngredientList!= null){for(int i = 0; i <mIngredientList.size(); i ++)adapter.add(mIngredientList.get(i)); }
訣竅是這樣的:public void onTextChanged(CharSequence charSequence,int i,int i1,int i2){
。mIngredientAdapter.getFilter()濾波器(charSequence.toString()); mItemList.setAdapter(mIngredientAdapter); }不重新添加適配器,似乎不起作用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.