安卓:在prepareDialogBu​​ilder,的onClick&安培; setItemChecked [英] Android: onPrepareDialogBuilder, onClick & setItemChecked

查看:271
本文介绍了安卓:在prepareDialogBu​​ilder,的onClick&安培; setItemChecked的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经一个星期花了试图找出一种方法做有限多重选择preference列表。没有什么我试过的作品。我准备放弃在Android上,如果事情看似简单的就是这么难。我一直在编程很长一段时间,不记得这样的事情被殴打严重这一点。我必须承担我不理解一些基本的东西。我希望有人能指出我在正确的方向。

I've spent over a week trying to figure out a way to do a Limited Multi Selection Preference list. Nothing I've tried works. I'm ready to give up on Android if something seemingly simple is so hard. I've been programming a long time and don't remember being beaten up this badly by something like this. I have to assume I am not understanding something basic. I hope someone can point me in the right direction.

下面是最简单的code我能想到的应该工作。它时,它设置为false,我已经试过也是如此甚至不清除该复选框。为什么不这项工作?如果这是行不通的,会是怎样?

Here is the simplest code I can think off that should work. It does not clear the checkbox even when setting it to false, I've tried true as well. Why doesn't that work? If that will not work, what will?

任何帮助将是最AP preciated。

Any help would be most appreciated.

    @Override
    protected void onPrepareDialogBuilder(Builder builder) 
    {
     CharSequence[] entries = getEntries();
     CharSequence[] entryValues = getEntryValues();

        if (entries == null || entryValues == null || entries.length != entryValues.length ) {
            throw new IllegalStateException(
                    "ListPreference requires an entries array and an entryValues array which are both the same length");
        }

        // Added by WJT since we are loading the entries values after instantiation 
        // we need the clicked indexes to be setup now, they would not have been
        // set up in the constructor
        if ((mClickedDialogEntryIndices == null) || (mClickedDialogEntryIndices.length == 0))
         mClickedDialogEntryIndices = new boolean[getEntries().length];

        restoreCheckedEntries();
        builder.setMultiChoiceItems(entries, mClickedDialogEntryIndices, 
                new DialogInterface.OnMultiChoiceClickListener() 
        {
   public void onClick(DialogInterface dialog, int which, boolean val) 
   {
    mDlg = (AlertDialog)getDialog();
                mListView = (ListView)mDlg.getListView();
    if (val)
             {
              if (mSelectedCount < mLimit)
                 {
               mClickedDialogEntryIndices[which] = val;
                  mSelectedCount++;

                 }
              else
                 {
                  mListView.setItemChecked(which, false);
               Toast.makeText(getContext(),
              R.string.newsLimitExceededMessage,
              Toast.LENGTH_LONG).show();

                 }  // (mSelectedCount < mLimit)

             }
             else
             {
              mClickedDialogEntryIndices[which] = val;
              mSelectedCount--;

             }  // (val)

   }  // void onClick(DialogInterface dialog, int which, boolean val)


        });  //  DialogInterface.OnMultiChoiceClickListener() 

    }  // void onPrepareDialogBuilder(Builder builder) 

谢谢,

\\ ^ / -111

\ ^ / i l l

推荐答案

下面是我会怎么处理这个问题:

Here's how I would approach the problem:

步骤1:在一个独立的一次性测试活动得到这个工作。忘记preferences。忘了对话。只专注于具有 CHOICE_MODE_MULTIPLE ListView控件的功能其中,被检查的项目有一定的数量之后,未选中的项被禁用。

Step #1: Get this working in a standalone throwaway test activity. Forget preferences. Forget dialogs. Just focus on the functionality of having a CHOICE_MODE_MULTIPLE ListView where, after a certain number of items are checked, the unchecked items become disabled.

第二步:从第1步在自定义窗口小部件的形式工作的功能。这样,我的意思是你会实现 ListView控件的子类(我猜的......如果有超过的ListView <更多的可能是一些容器/ code>),你从步骤#需要一个烘烤在所有的1。

Step #2: Get the functionality from Step #1 working in the form of a custom widget. By this, I mean you would implement a subclass of ListView (I guess...might be some container if there's more to it than a ListView) that bakes in all of what you need from Step #1.

第三步:创建一个自定义的对话preference 子类使用步骤#的自定义部件2

Step #3: Create a custom DialogPreference subclass that uses the custom widget from Step #2.

例如,这里是一个示例项目在那里我有一个自定义的 Col​​orMixer 部件,卷成颜色preference

For example, here is a sample project where I have a custom ColorMixer widget, rolled into a ColorPreference.

这篇关于安卓:在prepareDialogBu​​ilder,的onClick&安培; setItemChecked的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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