How to compare voice command input by user to hard coded strings

1k views Asked by At

I am currently working on an app that takes user input by voice command. The program recognizes the words using the google api. I want to be able to compare the user voice input to hard coded strings. My problem is that I do not know how to code this function. I am using if else statements to match the hard coded string, but the Toast is always the wrong one. Could anyone please guide me? Thank you for your time!

/**
 * Receiving speech input
 * */
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    switch (requestCode) {
    case REQ_CODE_SPEECH_INPUT: {
        if (resultCode == RESULT_OK && null != data) {

            ArrayList<String> result = data
                    .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
            txtSpeechInput.setText(result.get(0));

            if(txtSpeechInput.equals("weather")) {
                Toast.makeText(getApplicationContext(), "Good", Toast.LENGTH_SHORT).show();
            }else{

                Toast.makeText(getApplicationContext(), "It's a difficult question... I'm sorry", Toast.LENGTH_SHORT).show();


            }

        }

        break;
    }

    }
}
1

There are 1 answers

0
Jack On BEST ANSWER

You are comparing your String to a TextView object instead of directly to the data.

Try if( result.get(0).equals("weather") ) { ... }

Or at least if( txtSpeechInput.getText().toString().equals("weather") ) { ... }