Adapter.NotifyDataSetChanged 不起作用 [英] Adapter.NotifyDataSetChanged doesn't work
问题描述
我的 listView 有适配器,我想使用 NotifyDataSetChanged() 更新我的适配器,但它对我不起作用.这就是我在适配器中添加数据的方式:
I have adapter for my listView and i want to update my adapter using NotifyDataSetChanged(), but it doesn't work for me. That's how I add data in my adapter:
if (Messages.Count != 0) {
ChatAdapter = new BubbleAdapter (this, Messages);
if(listViewChat.Adapter == null) {
listViewChat.Adapter = ChatAdapter;
} else {
ChatAdapter.NotifyDataSetChanged();
}
}
但是就像我说的它不起作用...如果我使用这种方式更新,我的 listView 会滚动到顶部:
But like I said it does not work... If i update using this way, my listView is scrolling to top:
ChatAdapter = new BubbleAdapter (this, Messages);
listViewChat.Adapter = ChatAdapter;
推荐答案
首先,我要感谢堆栈的每一位贡献者.我敢肯定,我欠你的感激不尽.
First and foremost, I would like to thank each and everyone of the contributors of stack. I'm sure I owe you more gratitude than I can give.
这个问题占用了我太多时间.对于仍在寻找解决方案的人:
This question has taken way too much of my time. For the folks that are still looking for a solution:
performFilter 应保持原样.
the performFilter should remain as is.
publishResults 应该像这样简单:
the publishResults should be as simple as:
mIngredientList = (ArrayList) results.values;适配器清除();如果(mIngredientList != null){for (int i = 0; i < mIngredientList.size(); i++)适配器.添加(mIngredientList.get(i));}
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) {
the trick is this: public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
mIngredientAdapter.getFilter().filter(charSequence.toString());mItemList.setAdapter(mIngredientAdapter);}不重新添加适配器,似乎不起作用.
mIngredientAdapter.getFilter().filter(charSequence.toString()); mItemList.setAdapter(mIngredientAdapter); } Not re-adding the adapter, doesn't seem to work.
这篇关于Adapter.NotifyDataSetChanged 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!