In my project i will use voice recognition. In the codes of voice recognition, an arraylist returns on activity result. But i just want to take the first d开发者_高级运维ata in the list. i mean in the project users will use speak button, speak and then see the results. And then there will be continue button that shows the first data of the results. How can i pass the result parameter to another activity class. But not as the originally list, just a string.
I use this code
@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);
}
Then in the other activity you must to uget the bundle and extrat the string
Bundle bundle = getIntent().getExtras();
if( (bundle!=null) && (bundle.contains("RESULT"){
String text = bundle.getString("RESULT");
}
else{
//other things
return;
}
精彩评论