适配器 notifyDataSetChanged 不起作用 [英] adapters notifyDataSetChanged does not work

查看:43
本文介绍了适配器 notifyDataSetChanged 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑 2:

我确实解决了我的问题,但我不知道如何解决:我正在稍微移动我的代码片段,突然它起作用了.一定是按照错误的顺序做了一些事情,但很奇怪,检查了很多次.感谢您的帮助,很抱歉我无法发布答案;)

I did solve my problem, but i don't know how:S I was moving my code snippets a bit around, a suddenly it worked. Must have done something in the wrong order, but its weird, checked it many times. Thanks for you help, and sorry I can't post an answer ;)

嗨.

我有一个列表视图,当我向其中添加元素时,我试图刷新它以自我更新底层数组列表.

I have a list view which I'm trying to refresh to update it self when i add an element to the underlying array list.

这是代码片段:

private void addEvent() {
    arrlEvents.add( event );
    adptEvents.notifyDataSetChanged();
    updateSaveFile();
    filterList();
}

arrlEvents 是带有事件的底层数组列表,我添加了一个事件,尝试使用 notifyDataSetChanged() 更新列表视图,但它不起作用.有人可以帮忙吗?

The arrlEvents is the underlying arraylist with the events, and im adding one event, trying to update the list view with notifyDataSetChanged(), but it doesnt work. Can anyone help?

感谢您的时间:)

这是适配器的源代码:

private ArrayAdapter<Event> adptEvents;
adptEvents = new ArrayAdapter<Event>( EventCalendar.this, R.layout.list_items, arrlEvents );

推荐答案

我看到有时它只是随机不通知适配器.

I have seen that sometimes it just randomly doesnt notify the adapter.

尝试在全局范围内使用 adptEvents 作为 protected 或 public.

Try using adptEvents as protected or public on a global scope.

我发现当那不起作用时.您可以再次重新设置适配器,只需将 notifyDataSetChanged() 替换为:

I have found that when that doesnt work. You can just re set the adapter again, just substitute the notifyDataSetChanged() for:

adptEvents = new ArrayAdapter<Event>( EventCalendar.this, R.layout.list_items, arrlEvents );

这是我编写的一个有效的应用程序中的代码截图.

Heres a code snipper from an App I wrote that works.

类定义:

public class ClassName extends ListActivity implements AdapterView.OnItemSelectedListener {

全局变量:

CustomAdapter adapter;

在 OnCreate() 中:

in OnCreate():

adapter = new CustomAdapter(this,R.layout.layout_name,dataSet);
setListAdapter(adapter);

任何时候我需要通知

adapter.notifyDataSetChanged();

这篇关于适配器 notifyDataSetChanged 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆