如何选择复选框项?

时间:2014-12-12 09:43:51

标签: android json android-alertdialog

我收到来自服务器的回复,我使用textview,在textview的点击监听器上我设置了alertdialog,用户可以在其中选择多个项目,但是当服务器的值通过响应时,未选择项目,请参阅下面的图片

enter image description here

enter image description here

public class ButtonClickHandler implements View.OnClickListener {
    public void onClick(View view) {
        showDialog(0);
    }
}
@Override
protected Dialog onCreateDialog(int id) {
    return
            new AlertDialog.Builder(this)
                    .setTitle("Languages")
                    .setMultiChoiceItems(_options, _selections, new DialogSelectionClickHandler())
                    .setPositiveButton("OK", new DialogButtonClickHandler())
                    .create();
}

public class DialogSelectionClickHandler implements DialogInterface.OnMultiChoiceClickListener {
    public void onClick(DialogInterface dialog, int clicked, boolean selected) {
        Log.i("ME", _options[clicked] + " selected: " + selected);
    }
}

public class DialogButtonClickHandler implements DialogInterface.OnClickListener {
    public void onClick(DialogInterface dialog, int clicked) {
        switch (clicked) {
            case DialogInterface.BUTTON_POSITIVE:
                spmrg.setText("");
                for (int i = 0; i < _options.length; i++)
                {
                    if(_selections[i])
                    {
                        if(spmrg.getText().toString().length()>0)
                        {
                            spmrg.setText(spmrg.getText().toString().trim()+","+_options[i]);
                        }else
                        {
                            spmrg.setText(_options[i]);
                        }
                    }

                }
                break;
        }
    }
}
protected void printSelectedPlanets(){
    for( int i = 0; i < _options.length; i++ ){
        Log.i( "ME", _options[ i ] + " selected: " + _selections[i] );
    }
}

        protected CharSequence[] _options = {"UnMarried","Widow/Widower","Divorcee","Separated"};
        protected boolean[] _selections = new boolean[_options.length];

1 个答案:

答案 0 :(得分:0)

让我们说你没有结婚&#39;在你的json对象中。

所以,这就是你要做的 -

CharSequence[] _options = { "UnMarried", "Widow/Widower", "Divorcee",
                "Separated" };
        String[] valueFromJson = { "UnMarried", "Divorcee" }; // you will pass
                                                                // your
                                                                // value from
                                                                // JSON
                                                                // to this
                                                                // variable
        boolean[] isSelected = new boolean[_options.length];
        boolean[] _selections = new boolean[_options.length];

public boolean[] markChecked() {

        int[] locations = new int[valueFromJson.length];
        for (int i = 0; i < valueFromJson.length; i++) {
            int index = Arrays.asList(_options).indexOf(valueFromJson[i]);
            locations[i] = index;
            System.out.println(index + " " + valueFromJson[i]);

        }
        int k = 0;
        for (int i = 0; i < isSelected.length; i++) {
            if (k< locations.length && locations[k] == i) {
                isSelected[i] = true;
                k++;
            } else {
                isSelected[i] = false;
            }
            System.out.println(isSelected[i]);
        }
        return isSelected;

    }

现在这样做 -

.setMultiChoiceItems(_options, markChecked(), new DialogSelectionClickHandler())