2012-03-14 57 views
5

我试图检索值微调选择,我用下面的代码从微调器检索选定的项目?

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

回答

1

申报对象项目未来的OnCreate中之外,使其可用于整个类。

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) { 
    } 
}); 
} 
} 
+0

当我用最后它显示无效modifier.i已经使用了一个函数内部的微调在我的主类 – user1186739 2012-03-14 12:23:49

+0

你能告诉我在该方法中你从微调想要的吗? – 2012-03-14 12:24:58

+0

实现谷歌的语音识别在recognizor我project..so输出我想成为spinner..i内已编辑的问题/请chekout功能 – user1186739 2012-03-14 13:09:31

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]; 
2

使用最终目标,如:

final Object item; 

中的onCreate

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.Select从旋转

ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item, array_spinner); 
     spin.setAdapter(adapter); 

任何项目第4步:获取价值的选定值为

String anyvariable=String.valueOf(spin.getSelectedItem()); 
+0

我想使用所选的项目 – user1186739 2012-03-14 12:25:58

+0

你可以选择的值item as String anyvar = String.valueOf(spin.getSelectedItem()); – 2012-03-14 13:27:35

1

如果您不打算在用户选择时使用它,即调用一些方法,在你的onItemSelectedspinner.getSelectedItem()

这样在你的代码:

... 
//get object to use 
Object myObj = spinner.getSelectedItem(); 
// use object. 

否则定义OnItemSelectedListener以外的物体,像其他的答案。