我正在尝试检索所选的值微调器,并使用了以下代码
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
Object item = parent.getItemAtPosition(pos);
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
但我不能使用函数外的项的值!我是java的新手请有人帮助我.. 我试图在android中实现语音识别
public void speakButtonClicked(View v)
{
startVoiceRecognitionActivity();
}
/**
* Fire an intent to start the voice recognition activity.
*/
private void startVoiceRecognitionActivity()
{
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Voice recognition Demo...");
startActivityForResult(intent, REQUEST_CODE);
}
/**
* Handle the results from the voice recognition activity.
*/
@Override
protected
void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == REQUEST_CODE && resultCode == RESULT_OK)
{
// Populate the spinner with the String values the recognition engine thought it heard
ArrayList<String> matches = data.getStringArrayListExtra(
RecognizerIntent.EXTRA_RESULTS);
final Button button12=(Button)findViewById(R.id.button12);
final Spinner Speech_spinner=(Spinner)findViewById(R.id.spinner3);
Speech_spinner.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
matches));
Speech_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
Text = parent.getItemAtPosition(pos).toString();
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
button12.setText(Text);
}
super.onActivityResult(requestCode, resultCode, data);
}
button12
中没有值答案 0 :(得分:2)
使用最终对象,如:
final Object item;
在onCreate 之外的
答案 1 :(得分:1)
在oncreate之外声明Object项,以便它可供整个类使用。
答案 2 :(得分:1)
class Myactivity ...{
Object item ;
oncreate(...){
...
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
item = parent.getItemAtPosition(pos);
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
答案 3 :(得分:1)
使用pos值作为已添加到适配器的数组的索引并获取该值或
spnproj.setAdapter(new ArrayAdapter<String>(PaymentDetail.this, android.R.layout.simple_spinner_item,arr));
spnproj.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
String value =arr[arg2];
答案 4 :(得分:1)
步骤1.如果您已静态添加微调器,请使用Spinner spin=(Spinner)findViewById(R.id.spinner);
或动态地将其用作Spinner spin=new Spinner(YourActivity.this)
步骤2.添加数组中的项目以显示
array_spinner=new String[4];
array_spinner[0]="BFT";
array_spinner[1]="GFP";
array_spinner[2]="FSS";
array_spinner[3]="others";
步骤3.从微调器
中选择任何项目ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item, array_spinner);
spin.setAdapter(adapter);
步骤4.获取所选值的值
String anyvariable=String.valueOf(spin.getSelectedItem());
答案 5 :(得分:1)
如果您在用户选择时不打算使用它,即在onItemSelected
spinner.getSelectedItem()
在你的代码中这样:
...
//get object to use
Object myObj = spinner.getSelectedItem();
// use object.
否则,在OnItemSelectedListener
之外定义对象,就像其他答案一样。