我正在开发这个Android应用程序,在那里我得到语音重新填充列表视图,其中包含可识别单词的可能匹配项。
现在我想访问listview的第一个元素,但是我希望在listview填充后立即获取此值,而不是在我选择项目(或用户的任何其他活动)时。我想自动化这个。基本上我希望系统能够在识别出来后立即说出已被识别的单词。
据我所知,我需要创建一个arraylist存储已识别单词的值,然后将其传递给listview的适配器。
喜欢这个 -
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消息?我似乎无法弄清楚这一点,因为一旦识别开始应用程序崩溃...无论如何都要注入延迟以便识别器完成它的工作并且我成功地能够创建一个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>
我希望这会有所帮助。我真的被卡住了。
答案 0 :(得分:0)
listView.setOnScrollListener(this);
这给你listview第一项,
你必须implements OnScrollListener
并使用onscroll方法
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {}
在此方法中使用第一个可见项。
答案 1 :(得分: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...");
希望这有帮助...