1

我正在开发这个 android 应用程序,在该应用程序中,我获得语音记录以填充列表视图,其中可能与已识别的单词匹配。

现在我想访问列表视图的第一个元素,但我想在列表视图填充后立即获取此值,而不是在我选择项目(或用户的任何其他活动)时获取此值。我想自动化这个。基本上我希望系统一旦识别出已经识别出来的单词。

我知道我需要创建一个数组列表来存储已识别单词的值,然后将其传递给列表视图的适配器。

像这样 -

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));

现在,当我尝试通过以下方式访问此“匹配”数组列表时 -

String test = matches.toString();

但是,此时我遇到了一些错误,应用程序崩溃了。我无法让应用程序说出这个test字符串或从中创建一个 toast 消息。

请帮助解决此问题。

PS - 识别器是否有可能没有足够的时间来识别单词并填充列表视图,我尝试在识别完成之前从中发出祝酒消息?我似乎无法弄清楚这一点,因为一旦识别开始,应用程序就会崩溃......无论如何要注入延迟,以便识别器完成它的工作并且我能够成功地创建一个 toast 消息。

任何帮助将不胜感激。

添加代码 -

// 识别部分的代码,其中调用 ListenToSpeech 开始识别过程并填充 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);
}

// 我要实现的过程的代码 -

   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();               

            }
    }

这是我的 activity_main.xml 中的 listView

    <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"
 /> 

这是我的 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>

我希望这有帮助。我真的被困住了。

4

2 回答 2

0

您可以使用 listView.setOnScrollListener(this);

这给了你 listview 第一项,

你必须implements OnScrollListener

onscroll的使用方法

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

在这种方法中使用到第一个可见项目。

于 2013-11-09T10:00:13.613 回答
0

我假设您在 R.layout.words 中使用 TextView 来显示字符串,并且我假设 TextView id = myTextViewId。你可以试试这个:

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...");

希望这有帮助...

于 2013-11-09T10:09:41.230 回答