I am trying to compare the word obtained using Speech To Text, with the specified word in the condition. The idea is as follows: if the word matches the given one, then it appears, if not, then it does not appear. After I write the code with the addition of the condition, the word is not displayed, if the condition I remove appears.

public class MainActivity extends AppCompatActivity implements View.OnClickListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button recognizeButton = (Button)findViewById(R.id.button1); recognizeButton.setOnClickListener(this); } @Override public void onClick(View v) { Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "You may speak!"); intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1); intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.ENGLISH); startActivityForResult(intent, 1); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == 1 && resultCode == RESULT_OK) { ArrayList<String> results; results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); TextView speechText = (TextView) findViewById(R.id.textView1); //if (results.equals("car")) { speechText.setText(results.toString()); // } } } 

I mean that the error in the if condition, it is necessary to let the results know that it toString before the condition, and not in it, tried to change it, but something does not work.

    1 answer 1

    In this line

     results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); 

    you get an array, then

     speechText.setText(results.toString()); 

    you cast an array to a string. Hence the brackets. Go through all the elements of the array

     results[i].toString() 

    where i is a counter from 0 to results.size ()

     @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == 1 && resultCode == RESULT_OK) { ArrayList<String> results; results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); TextView speechText = (TextView) findViewById(R.id.textView1); String str=""; for(int i=0;i<results.size();i++){ str+=results.get(i); } if (str.equals("car")) { speechText.setText(str); } } } 
    • Thank you Only in the for loop I asked to replace str + = results [i]; on str + = results.get (i); - Nikolai
    • Yes, really hurried) - Vitalii Obideiko