简体   繁体   中英

Accessing listview items without onclicklistener in android

I am working on this android app where I get the speech reco to populate a listview with possible matches for the recognized word.

Now I want to getaccess to the first element of the listview but i want to get this value as soon as the listview populates and not when I select the item ( or any other activity from the user). I want to automate this. Basically I want the system to speak out the words that have been recognized as soon as they are recognized.

I understand that I need to create an arraylist stores the values of the recognized words and then passes it on to the adapter of the listview.

Like this -

matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
//set the retrieved list to display in the ListView using an ArrayAdapter
wordList.setAdapter(new ArrayAdapter<String> (this, R.layout.words, matches));

Now when I try to access this "matches" array list by -

String test = matches.toString();

However I am getting some error at this point and the app is crashing. I am not able to either make the app speak this test string or create a toast message out of it.

Please help in resolving this issue.

PS - Is it possible that the recognizer is not getting enough time to recognize the words and populate the listview and I try to make a toast message out of it before the recognition finishes? I can't seem to figure this out since the app crashes as soon as the recognition starts...Is there anyway to inject a delay so that the recognizer finishes it job and I am successfully able to create a toast message.

Any help will be greatly appreciated.

Adding the code -

// code for the recognition part where the call to ListenToSpeech starts the recognition process and fills up the wordList.

private void listenToSpeech() {

    //start the speech recognition intent passing required data
    Intent listenIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    //indicate package
    listenIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getClass().getPackage().getName());
    //message to display while listening
    listenIntent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Say a word!");
    //set speech model
    listenIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    //specify number of results to retrieve
    listenIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 10);

    Toast.makeText(MainActivity.this, "SpeakDestination", Toast.LENGTH_SHORT).show();

    //start listening
    startActivityForResult(listenIntent, VR_REQUEST);


}

/**
 * onActivityResults handles:
 *  - retrieving results of speech recognition listening
 *  - retrieving result of TTS data check
 */
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    //check speech recognition result 
    if (requestCode == VR_REQUEST && resultCode == RESULT_OK) 
    {
        //store the returned word list as an ArrayList
        matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
        //set the retrieved list to display in the ListView using an ArrayAdapter
        wordList.setAdapter(new ArrayAdapter<String> (this, R.layout.words, matches));
    }

    //returned from TTS data check
    if (requestCode == MY_DATA_CHECK_CODE) 
    {  
        //we have the data - create a TTS instance
        if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS)  
            repeatTTS = new TextToSpeech(this, this);  
        //data not installed, prompt the user to install it  
        else 
        {  
            //intent will take user to TTS download page in Google Play
            Intent installTTSIntent = new Intent();  
            installTTSIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);  
            startActivity(installTTSIntent);  
        }  
    }

    //call superclass method
    super.onActivityResult(requestCode, resultCode, data);
}

// code for the procedure I want to implement -

   private void dialogueControl(){

        if(initialDialogueCount<=1 && flagVar[0]==false){
            initialDialogue();
            if(initialDialogueCount>0){
                boolean speakingEnd = repeatTTS.isSpeaking();
                do{
                   speakingEnd = repeatTTS.isSpeaking();
                } while (speakingEnd);  
                Toast.makeText(MainActivity.this, "Destination", Toast.LENGTH_SHORT).show();
                listenToSpeech();    

            }

            Toast.makeText(MainActivity.this, "Something wrong here Destination", Toast.LENGTH_SHORT).show();


            initialDialogueCount++;
            if(initialDialogueCount>=2)
                flagVar[0]=true;
        }
        if(flagVar[0]==true){


                    // test is the string that I want to get from the wordList. The commented part here is what I want to do after I get that string. I want to speak that word out and ask for another input via the recognition.  The toast messages above are just to some debugging.                         


                String test = "";   

                //String ttsText = "You want to fly to "+test+" . Is that correct? Please say Yes or Noo?";
                Toast.makeText(MainActivity.this, test, Toast.LENGTH_SHORT).show();
//              if (ttsText!=null) {
//                  if (!repeatTTS.isSpeaking()) {
//                      repeatTTS.speak(ttsText, TextToSpeech.QUEUE_FLUSH, null);
//                  }
//              }
//              
//              do{
//                 speakingEnd = repeatTTS.isSpeaking();
//              } while (speakingEnd);  
//              
//              flagVar[1]=true;
//              
//              Toast.makeText(MainActivity.this, "Destination", Toast.LENGTH_SHORT).show();
//              
//              listenToSpeech();               

            }
    }

This is the listView in my activity_main.xml

    <ListView android:id="@+id/wordList"
    android:layout_width="fill_parent"
    android:layout_height="0dip"
    android:layout_weight="1"
    android:paddingLeft="10dp"
    android:paddingTop="3dp"
    android:paddingRight="10dp"
    android:paddingBottom="3dp"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:layout_marginTop="5dp"
    android:layout_marginBottom="5dp"
 /> 

and this is my words.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:padding="5dp"
android:textSize="16sp" >
</TextView>

I hope this helps. I am really stuck.

you can use listView.setOnScrollListener(this);

this give u listview first item,

you have to implements OnScrollListener

and use method of onscroll

@Override
public void onScroll(AbsListView view, int firstVisibleItem,
        int visibleItemCount, int totalItemCount) {}

in this method use to firstvisible item.

I assumed that you use TextView inside R.layout.words to show the string, and I assumed the TextView id = myTextViewId. You could try this:

int position = 0; // YOUR_LISTVIEW_POSITION you want to access, 0 is the first position
TextView myTextView = (TextView) wordList.getChildAt(position).findViewById(R.id.myTextViewId);

// doing whatever you want, example: change the text value
myTextView.setText("HORAY...");

hope this help...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM