0

我正在尝试使用动态数据构建多选 AlertDialog。

好的,一切都很好,等等,但是选择搞砸了。

这是我的代码:

new AlertDialog.Builder(this)
        .setTitle("Cities")
        .setMultiChoiceItems(_options, _selections, new DialogInterface.OnMultiChoiceClickListener(){

                @Override
                public void onClick(DialogInterface dialog, int clicked, boolean selected) {
                        Log.i("Database", _options[clicked] + " selected: " + selected);
                }

        })
        .setPositiveButton("OK", new DialogInterface.OnClickListener(){

                @Override
                public void onClick(DialogInterface dialog, int clicked) {
                    switch(clicked) {
                        case DialogInterface.BUTTON_POSITIVE:
                            for( int i = 0; i < _options.length; i++ ){
                                Log.i("Database", "id: " + _values[i] + " " + _options[i] + " selected: " + _selections[i]);
                            }
                            break;
                    }
                }
        })
        .create();

正如您在 LogCat 中看到的,我描述了我的问题:

** OPENED ALERTDIALOG VIA A BUTTON AND SELECTED THE FOLLOWING: **
07-12 16:06:51.347: I/Database(8034): Aveiro selected: true
07-12 16:06:53.936: I/Database(8034): Coimbra selected: true
07-12 16:07:00.116: I/Database(8034): Porto selected: true

** AFTER PRESSING THE OK BUTTON, THIS SHOWS UP, WHICH IS CORRECT: **
07-12 16:07:02.826: I/Database(8034): id: 1 Aveiro selected: true
07-12 16:07:02.826: I/Database(8034): id: 2 Coimbra selected: true
07-12 16:07:02.826: I/Database(8034): id: 3 Porto selected: true
07-12 16:07:02.826: I/Database(8034): id: 4 Minho selected: false

** I CLICKED THE BUTTON TO START THE DIALOG AGAIN DE UNSELECTED THE FOLLOWING: **
07-12 16:07:07.087: I/Database(8034): Coimbra selected: false

** AFTER PRESSING THE OK BUTTON, ALL SHOWS AS FALSE. 1 AND 3 SHOULD BE TRUE: **
07-12 16:07:08.097: I/Database(8034): id: 1 Aveiro selected: false
07-12 16:07:08.097: I/Database(8034): id: 2 Coimbra selected: false
07-12 16:07:08.097: I/Database(8034): id: 3 Porto selected: false
07-12 16:07:08.097: I/Database(8034): id: 4 Minho selected: false
4

2 回答 2

1

编辑

尝试了您的代码,它对我来说工作正常。逐步检查并确保您的值是正确的。

这是我的测试应用程序。唯一的区别是我不知道是什么_values,所以我把它拿出来了。

public class SandBoxActivity extends Activity {

    private Button testButton;

    private CharSequence[] _options = {"Aveiro", "Coimbra", "Porto", "Minho"};

    private boolean[] _selections = {true, true, true, false};

    private AlertDialog test;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        test = new AlertDialog.Builder(this)
        .setTitle("Cities")
        .setMultiChoiceItems(_options, _selections, new DialogInterface.OnMultiChoiceClickListener(){

                @Override
                public void onClick(DialogInterface dialog, int clicked, boolean selected) {
                        Log.i("Database", _options[clicked] + " selected: " + selected);
                }

        })
        .setPositiveButton("OK", new DialogInterface.OnClickListener(){

                @Override
                public void onClick(DialogInterface dialog, int clicked) {
                    switch(clicked) {
                        case DialogInterface.BUTTON_POSITIVE:
                            for( int i = 0; i < _options.length; i++ ){
                                Log.i("Database", "id: " + " " + _options[i] + " selected: " + _selections[i]);
                            }
                            break;
                    }
                }
        })
        .create();

        this.testButton = (Button) findViewById(R.id.color_button);
        this.testButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                test.show();
            }
        });
    }
}

旧帖子

.setMultiChoiceItems(_options, _selections, new DialogInterface.OnMultiChoiceClickListener(){

                @Override
                public void onClick(DialogInterface dialog, int clicked, boolean selected) {
                        Log.i("Database", _options[clicked] + " selected: " + selected);
                }

        })

我没有看到您实际上正在处理它被点击的事实。_selections我看到您正在打印它被点击的日志,但您并没有改变它在您的阵列中被选中的事实。那可能是你的问题。

试试这个:

.setMultiChoiceItems(_options, _selections, new DialogInterface.OnMultiChoiceClickListener(){

                @Override
                public void onClick(DialogInterface dialog, int clicked, boolean selected) {
                        _selections[clicked] = selected;
                        Log.i("Database", _options[clicked] + " selected: " + selected);
                }

        })
于 2012-07-12T15:43:48.710 回答
1

Silentw,请阅读

每次创建时,我们都必须为所选选项提供布尔数组

还更新您的代码如下

  public void onClick(DialogInterface dialog, int clicked, boolean selected) 
{                      
                     if(!_selections[clicked])
                        _selections[clicked] = true;
                    else
                        _selections[clicked]=false;

                        Log.i("Database", _options[clicked] + " selected: " + selected);

}
于 2012-07-12T15:53:42.983 回答