2011-08-25 11 views
0

在我的項目中,我將使用語音識別。在語音識別代碼中,數組列表返回活動結果。但我只想將列表中的第一個數據。我的意思是在項目中用戶將使用說話按鈕,說話,然後看到結果。然後會顯示繼續按鈕,顯示結果的第一個數據。我如何將結果參數傳遞給另一個活動類。但不是最初的列表,只是一個字符串。以語音識別結果的第一筆數據

回答

0

我在其他活動中使用此代碼

@Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if (requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK) { 
      ArrayList<String> matches = new ArrayList<String>(); 
      matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); 


      if(matches.lenght()>0) { 
       result = matches.get(0) 

       //Time to create the new intent to the other activity 
       Intent i = new Intent (Activity1.this, Activity2.class); 
       i.putExtra("RESULT", result); //RESULT is the key 
       startActivityForResult(i,ID); // or startActivity(i) 
      } 
     } 

     super.onActivityResult(requestCode, resultCode, data); 
    } 

然後您必須uget捆綁和extrat字符串

Bundle bundle = getIntent().getExtras(); 
    if((bundle!=null) && (bundle.contains("RESULT"){ 
     String text = bundle.getString("RESULT"); 

    } 
    else{ 
     //other things 
     return; 
    } 
+0

非常感謝你。它給出錯誤(對於類型ArrayList ,方法lenght()未定義)。你定義了什麼結果? –

+0

對不起,我寫的代碼很快,我讓你瞭解代碼。 matches.size()檢查數組的長度/大小。 字符串結果 – Aracem

+0

如果答案正確,您應該標記爲已接受的答案;) – Aracem