我在弹出窗口中有一个按钮,当点击它时会初始化一个警告对话框,其中包含一个用户可以从中选择的列表。我试图从列表中获取所选项目的字符串值。我试图获取该项目,然后更改按钮上的说明文字以反映用户的选择。
countryButton.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
final ArrayAdapter<CharSequence> countryAdapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.countries_array, android.R.layout.simple_spinner_item);
new AlertDialog.Builder(MakeQuestion.this)
.setTitle("Country")
.setAdapter(countryAdapter, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//String countryResult = countryList.get(which);
//countryButton.setText(countryResult);
dialog.dismiss();
}
}).create().show();
}
});
答案 0 :(得分:1)
您必须使用ArrayAdapter.getItem()方法。如果它不仅仅是一个复制粘贴错误,请不要忘记@Override注释。但你对“缺少参考错误”是什么意思?
countryButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
final ArrayAdapter<CharSequence> countryAdapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.countries_array, android.R.layout.simple_spinner_item);
new AlertDialog.Builder(MakeQuestion.this)
.setTitle("Country")
.setAdapter(countryAdapter, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String countryResult = countryAdapter.getItem(which);//use this getItem() method
countryButton.setText(countryResult);
dialog.dismiss();
}
}).create().show();
}
});