在Android中设置ListView警报对话框的默认选定项 [英] Set default selected item of ListView Alert Dialog in Android
问题描述
我创建了一个带有自定义ListView
的对话框,该对话框模拟了Spinner
的显示,并且最初以"Select Gender"值开始.
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
(我希望选择男性",因为这是他们的最后选择)
到目前为止,这是我的代码:
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)
未将所选默认设置设置为男性"
genderAlert.getListView().setSelection(1)
未将所选默认设置设置为女性"
genderAlert.getListView().setSelection(0)
doesn't set the default selected as "Male"
genderAlert.getListView().setSelection(1)
doesn't set the default selected as "Female"
推荐答案
弄清楚了:
我从.setAdapter
切换到.setSingleChoiceItems
,其中有一个用于默认选择的参数.然后,我要做的就是创建一个全局变量,每次单击一个选项时都会对其进行设置.全局变量最初设置为-1,因此未选择任何选项,然后在单击某个对象时将其设置为位置,并在下次创建对话框时,该选择反映了我先前的选择.见下文:
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;
}
});
这篇关于在Android中设置ListView警报对话框的默认选定项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!