Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set default selected item of ListView Alert Dialog in Android

I have created a dialog with a custom ListView that models a Spinner display, and originally it starts out with the value "Select Gender".

When the dialog opens it prompts for a selection (just like a spinner). If the selection gets selected again, it shows the same options, but doesn't indicate which option has already been selected.

Example:
Default Value: "Select Gender"
Dialog opens with no selection
User selects: "Male"
User reopens dialog...
Dialog opens with no selection

(I'd like it to have "Male" selected, since that was their last selection)

Here's my code so far:

genderItems = getResources().getStringArray(R.array.gender_array);
genderAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, genderItems);

genderDrop.setOnTouchListener(new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            if(event.getAction() == MotionEvent.ACTION_DOWN) {
                Builder genderBuilder = new AlertDialog.Builder(Register.this)
                    .setTitle(R.string.gender_prompt)
                    .setAdapter(genderAdapter, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        inputGender.setText(genderItems[which]);
                        dialog.dismiss();
                    }
                });
                AlertDialog genderAlert = genderBuilder.create();
                genderAlert.show();
                genderAlert.getListView().setSelection(0);
            }
            return false;
        }
});

genderAlert.getListView().setSelection(0) doesn't set the default selected as "Male"
genderAlert.getListView().setSelection(1) doesn't set the default selected as "Female"

like image 895
Rawr Avatar asked Aug 30 '12 02:08

Rawr


1 Answers

Figured it out:

I switched from .setAdapter to .setSingleChoiceItems which has an argument for the default selection. Then all I had to do was create a global variable that got set each time I clicked an option. The global variable is initially set to -1 so no option is selected, then once I click on something it gets set to the position and the next time the dialog is created the selection reflects my previous selection. See below:

Integer selection = -1;

genderItems = getResources().getStringArray(R.array.gender_array);
genderAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, genderItems);

genderDrop.setOnTouchListener(new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            if(event.getAction() == MotionEvent.ACTION_DOWN) {
                Builder genderBuilder = new AlertDialog.Builder(Register.this)
                    .setTitle(R.string.gender_prompt)
                    .setSingleChoiceItems(genderAdapter, selection, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            inputGender.setText(genderItems[which]);
                            selection = which;
                            dialog.cancel();
                        }
                    });
                AlertDialog genderAlert = genderBuilder.create();
                genderAlert.show();
            }
            return false;
        }
});
like image 157
Rawr Avatar answered Oct 11 '22 03:10

Rawr