ListView的警告对话框中设置所选项目 [英] Set selected item of ListView Alert Dialog
问题描述
所以,我创建一个自定义列表视图的模型中飞旋显示一个对话框,并且它最初开始时,其值为性别选择。
So I create 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
(我想它有男选择,因为这是他们最后的选择)
下面是我的code到目前为止:
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,所以没有选项被选中,然后当我点击的东西它被设置的位置和创建对话框中的选择反映了我的previous选择下一次。请看下图:
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;
}
});
这篇关于ListView的警告对话框中设置所选项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!