user2342687
user2342687

Reputation: 227

How can I get a specific item only in an Android ListView?

I want to get a specific item in an Android ListView. ListView should be filled from a Speech Recognizer function. How can I do this without click auto? I try something but not work. Any help will be appreciated!

protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    if (requestCode == REQUEST_CODE && resultCode == RESULT_OK)
    {
        // Populate the wordsList with the String values the recognition engine thought it heard
        ArrayList<String> matches = data.getStringArrayListExtra(
                RecognizerIntent.EXTRA_RESULTS);
        wordsList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
                matches));

        String info =  wordsList.getItemAtPosition(0).toString();

        if(info.indexOf("Hello")>0){

            Toast.makeText(getBaseContext(),"OK", Toast.LENGTH_LONG).show();

        }

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

Upvotes: 0

Views: 228

Answers (2)

Ken Wolf
Ken Wolf

Reputation: 23269

No need to go through the adapter/listView since you already have a List of Strings.

Change

String info =  wordsList.getItemAtPosition(0).toString();

To

String info =  matches.get(0);

It will get the first item in your list, which is what I understand you are trying to do.

Upvotes: 1

Kishor datta gupta
Kishor datta gupta

Reputation: 1103

not test, but it could work

 ArrayAdapter<String> ad = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
                    matches));

            String info =  ad.getItemAtPosition(0).toString();

            if(info.indexOf("Hello")>0){

                Toast.makeText(getBaseContext(),"OK", Toast.LENGTH_LONG).show();

            }

Upvotes: 0

Related Questions