简体   繁体   English

Adapter.NotifyDataSetChanged不起作用

[英]Adapter.NotifyDataSetChanged doesn't work

I have adapter for my listView and i want to update my adapter using NotifyDataSetChanged(), but it doesn't work for me. 我的listView有适配器,并且我想使用NotifyDataSetChanged()更新适配器,但它对我不起作用。 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();
    }
}

But like I said it does not work... If i update using this way, my listView is scrolling to top: 但是就像我说的那样,它不起作用...如果我使用这种方式进行更新,我的listView会滚动到顶部:

ChatAdapter = new BubbleAdapter (this, Messages);
listViewChat.Adapter = ChatAdapter;

Please look at below code. 请看下面的代码。 You have to do similar to what I mentioned below. 您必须执行与我在下面提到的类似的操作。 As I don't have your whole code snippet, so I modified the codes you mentioned: 由于我没有完整的代码段,因此我修改了您提到的代码:

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();
}

Plase let me know if you still not succeeded. 请让我知道您是否仍然没有成功。

Try: 尝试:

listViewChat.setAdapter(ChatAdapter);

You can replace the entire block with that. 您可以用它替换整个块。 If the list is empty the adapter will be empty as well, and the listview will be refreshed with zero items. 如果列表为空,则适配器也将为空,并且将以零项刷新listview。

Use ChatAdapter.notifyDataSetInvalidated(); 使用ChatAdapter.notifyDataSetInvalidated(); instead of ChatAdapter.notifyDataSetChanged(); 而不是ChatAdapter.notifyDataSetChanged();

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: 对于仍在寻找解决方案的人们:

  1. the performFilter should remain as is. performFilter应该保持原样。

  2. the publishResults should be as simple as: publishResults应该很简单:

    mIngredientList = (ArrayList) results.values; mIngredientList =(ArrayList)results.values; adapter.clear(); adapter.clear(); if (mIngredientList != null) { for (int i = 0; i < mIngredientList.size(); i++) adapter.add(mIngredientList.get(i)); if(mIngredientList!= null){for(int i = 0; i <mIngredientList.size(); i ++)adapter.add(mIngredientList.get(i)); } }

  3. the trick is this: public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { 诀窍是这样的:public void onTextChanged(CharSequence charSequence,int i,int i1,int i2){

    mIngredientAdapter.getFilter().filter(charSequence.toString()); 。mIngredientAdapter.getFilter()滤波器(charSequence.toString()); mItemList.setAdapter(mIngredientAdapter); mItemList.setAdapter(mIngredientAdapter); } Not re-adding the adapter, doesn't seem to work. }不重新添加适配器,似乎不起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM