ListView控件不显示更改,直到notifyDataSetChanged后更改焦点 [英] ListView does not show changes until focus changes after notifyDataSetChanged

查看:154
本文介绍了ListView控件不显示更改,直到notifyDataSetChanged后更改焦点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 AlertDialog 的ListView 设置为多选就可以了。它也有一个按钮就可以了。

I have an AlertDialog with a ListView set to multiple selection on it. It also has a Button on it.

按钮打开另一个 AlertDialog ,如果ok'ed将来自的数据集删除选定的项目的ListView ,然后告诉列表视图中的数据集已经与 notifyDataSetChanged()方法改变适配器。

The Button open another AlertDialog that if ok'ed will remove the selected items from the data set of the ListView, and then tell the adapter of the list view that the dataset has changed with the notifyDataSetChanged() method.

这一切工作正常,除了一件事。该的ListView 不更新它的内容,直到我的东西互动。然后,它更新到正确的数据。

This all works fine except for one thing. The ListView does not update it's content until I interact with something. Then it updates to the correct data.

这是不是一个大问题,但我真的希望的ListView 来显示正确的一次,而不是关注的焦点更改后。

This is not a big problem, but I really would like the ListView to appear correct at once, and not just after the focus has changed.

code:

Button remove = (Button) view.findViewById(R.id.btn_remove_questions_edit_rack);
    final Context con = this;
    remove.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            Builder warnBuild = new Builder(con);
            warnBuild.setMessage(R.string.question_deletion_warning);
            warnBuild.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener()
            {
                @Override
                public void onClick(DialogInterface dialog, int which)
                {
                    SparseBooleanArray checked = list.getCheckedItemPositions();
                    for (String s : keys)
                    {
                        int i = keys.indexOf(s);
                        if (checked.get(i))
                        {
                            toRemove.add(map.get(s));
                            map.remove(s);
                        }
                    }
                    keys.clear();
                    keys.addAll(map.keySet());
                    ((ArrayAdapter) list.getAdapter()).notifyDataSetChanged();
                    list.clearChoices(); //This makes sure the selection is cleared, if it isn't, some of the other items (those that now has the index of the selected items) will be selected when the View refreshes.
                    dialog.dismiss();
                }
            });

            //Negative button here, not relevant.
        }
    });

其中,地图是:

final HashMap<String, QualityQuestion> map = new HashMap<>();
//I add items to the map
final ArrayList<String> keys = new ArrayList<>(map.keySet());

的文档,删除是我店从他们是在当上了原来的OK按钮 AlertDialog 是pressed。

And toRemove is where I store the items to be removed from the actual object they are on when the ok button on the original AlertDialog is pressed.

这是我如何填充我的摆在首位的ListView

final ListView list = (ListView) view.findViewById(R.id.list_questions_edit_rack);
    list.setAdapter(
            new ArrayAdapter<String>(this,
                    android.R.layout.simple_list_item_activated_1,
                    keys));

我已经试过像 list.invalidateViews() list.invalidate ,我发现了问题,其他的事情的东西同样在这里开采的左右。但这些都不是什么不同。我怀疑我的问题是与他们不同,因为我的项目显然被更新,它只是需要注重对原有的改变 AlertDialog 的变化是明显的。

I have tried things like list.invalidateViews(), list.invalidate and other things I found in questions similar to mine here on SO. But none of that made any difference. I suspect my problem to be different from theirs since my items clearly are updated, it just takes a change of focus on the original AlertDialog for the change to be visible.

我怎样才能让的ListView 显示的变化,它的数据源焦点更改后imi​​diatly insted的呢?

How can I make the ListView show the changes in it's data source imidiatly insted of after a focus change?

推荐答案

通过调用

((ArrayAdapter) list.getAdapter()).notifyDataSetChanged();

你会得到一个新的适配器,它几乎肯定不完全相同用来填充在一审您的清单匿名适配器。

you get a fresh adapter which is almost certainly not identical to the anonymous adapter you used to populate your list in the first instance.

另请参阅 ListView.getAdapter的文档()

在此的ListView当前使用返回适配器。   返回的适配器可能不会传递到setAdapter同一个适配器(ListAdapter),但可能是一个WrapperListAdapter。

Returns the adapter currently in use in this ListView. The returned adapter might not be the same adapter passed to setAdapter(ListAdapter) but might be a WrapperListAdapter.

从这一新的接口点,该数据集并没有改变,因为发生了变化方式,它被实例化之前。

From the point of view of this fresh adapter, the data set hasn't changed because the changes happened way before it was instantiated.

要解决你的问题,让你的清单,你的活动类(或范围要延续他们的生命)的列表适配器的成员:

To solve your problem, make your list and your list adapter members of your activity class (or the scope where you want to keep them alive):

private ArrayList<String>    keys;
private ArrayAdapter         myAdapter;
private ListView             list;

然后在你的的onCreate()

Then in your "onCreate()"

keys = ...;     // initialization of ArrayList with the needed data 
myAdapter = new ArrayAdapter<String>(this,
                    android.R.layout.simple_list_item_activated_1,
                    keys);
list = (ListView) view.findViewById(R.id.list_questions_edit_rack);
list.setAdapter(myAdapter);

这样一来,在你的OnClickListener你可以通知myAdapter:

This way, in your "OnClickListener" you can notify "myAdapter":

keys.addAll(map.keySet());
myAdapter.notifyDataSetChanged();

希望这有助于:)

Hope this helps :)

这篇关于ListView控件不显示更改,直到notifyDataSetChanged后更改焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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