简体   繁体   中英

How to make selected checkbox item?

I am getting response from server,and I use textview,and on textview's click listener I set alertdialog,where user can select multiple items,but when values from server via reponse,items are not selected see images below

在此处输入图片说明

在此处输入图片说明

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];

Lets say you got 'unmarried' in your json object.

So, this is what you will do -

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;

    }

now do this -

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM