警报对话框中的多项选择

发布于 2024-10-20 05:03:02 字数 1288 浏览 1 评论 0原文

我正在关注以下问题。 我有一个带有 setMultipleChoiceItems 的警报对话框,已创建对话框 并正确显示,但是当我尝试取消选中任何选定的 项目,该项目保持选中状态。 以下是代码片段:

SimpleCursorAdapter adapter = new SimpleCursorAdapter(context, 
                        android.R.id.text1, 
                        c, 
                        new String[] {label}, 
                        new int[] {android.R.id.text1} 
            ); 
            AlertDialog dialog=new AlertDialog.Builder(context) 
               .setTitle(title) 
               .setPositiveButton(R.string.okBtn, null) 
               .setNegativeButton(R.string.cancelBtn, null) 
               .setMultiChoiceItems(c,state,label, 
               new DialogInterface.OnMultiChoiceClickListener() { 
                            public void onClick(DialogInterface dialog, int which, 
                                            boolean isChecked) { 
                                    Log.v("TEST", "onClick(..) called with value " + which +
                                       " / "+ isChecked); 
                            } 
               }) 
               .create(); 
             dialog.show() 

尽管如此,方法 OnMultiChoiceClickListener() 被调用并在日志 I 中 可以看到: “onClick(..) 调用值为 2 / false” 所以它说所选项目应该是FALSE(未选中)但是 对话框不会更新并且项目保持选中状态。 有什么想法为什么会这样吗?

I'm focusing following problem.
I have an alert dialog with setMultipleChoiceItems, dialog is created
and shown correctly but when I try to uncheck any of the selected
items, the item stays checked.
Here is the snippet of the code:

SimpleCursorAdapter adapter = new SimpleCursorAdapter(context, 
                        android.R.id.text1, 
                        c, 
                        new String[] {label}, 
                        new int[] {android.R.id.text1} 
            ); 
            AlertDialog dialog=new AlertDialog.Builder(context) 
               .setTitle(title) 
               .setPositiveButton(R.string.okBtn, null) 
               .setNegativeButton(R.string.cancelBtn, null) 
               .setMultiChoiceItems(c,state,label, 
               new DialogInterface.OnMultiChoiceClickListener() { 
                            public void onClick(DialogInterface dialog, int which, 
                                            boolean isChecked) { 
                                    Log.v("TEST", "onClick(..) called with value " + which +
                                       " / "+ isChecked); 
                            } 
               }) 
               .create(); 
             dialog.show() 

Althought, method OnMultiChoiceClickListener() is called and in log I
can see:
"onClick(..) called with value 2 / false"
so it says that chosen item is supposed to be FALSE (unchecked) but
the dialog is not updated and item stays checked.
Any ideas why it is like this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

离线来电— 2024-10-27 05:03:02

因此:http://code.google.com/p/android /issues/detail?id=2998

至少据我了解,当选中/取消选中该项目时,您必须更新数据库。就我个人而言,我不喜欢这个想法,对于多项选择列表,我使用 ListArrays 而不是游标:

        final Cursor mCursor = mDbAdapter.getAllData();
        final CharSequence[] names = new CharSequence[mCursor.getCount()];
        final boolean[] selected = new boolean[mCursor.getCount()];
        mCursor.moveToFirst();
        while (!mCursor.isAfterLast()) {
            names[it] = mCursor.getString(0);
            selected[it] = mCursor.getString(1);                
            mCursor.moveToNext();
        }
        mCursor.close();

        mBuilder.setMultiChoiceItems(names, selected, new DialogInterface.OnMultiChoiceClickListener() {
            public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                Toast.makeText(getApplicationContext(), names[which] + ": " + ((isChecked) ? "on" : "off"), Toast.LENGTH_SHORT).show();
            }
        });

这不是最好的方法,但我不知道更好的方法。

Because of this: http://code.google.com/p/android/issues/detail?id=2998

At least as far as I understand it, you have to update you database when the item is checked/unchecked. Personally, I don't like this idea and for multiple choice lists I use ListArrays instead of cursors:

        final Cursor mCursor = mDbAdapter.getAllData();
        final CharSequence[] names = new CharSequence[mCursor.getCount()];
        final boolean[] selected = new boolean[mCursor.getCount()];
        mCursor.moveToFirst();
        while (!mCursor.isAfterLast()) {
            names[it] = mCursor.getString(0);
            selected[it] = mCursor.getString(1);                
            mCursor.moveToNext();
        }
        mCursor.close();

        mBuilder.setMultiChoiceItems(names, selected, new DialogInterface.OnMultiChoiceClickListener() {
            public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                Toast.makeText(getApplicationContext(), names[which] + ": " + ((isChecked) ? "on" : "off"), Toast.LENGTH_SHORT).show();
            }
        });

It is not the best way to do it, but I don't know any better.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文