[英]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. 我正在使用这个Android应用程序,在该应用程序中,我得到了语音记录,以便在列表视图中填充与所识别单词的可能匹配项。
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. 我了解我需要创建一个存储已识别单词的值的arraylist,然后将其传递给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. 我无法使应用说出这个test
字符串,也无法从中创建吐司消息。
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? PS-识别器是否可能没有足够的时间来识别单词并填充列表视图,而我试图在识别完成之前用它吐司消息? 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. 我似乎无法弄清这一点,因为应用程序会在识别开始后立即崩溃。。。无论如何,是否存在延迟,以便识别器完成其工作,并且我能够成功创建Toast消息。
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. //识别部分的代码,其中对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);
}
// 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 这是我的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"
/>
and this is my words.xml 这是我的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);
您可以使用listView.setOnScrollListener(this);
this give u listview first item, 这给你listview第一项,
you have to implements OnScrollListener
你必须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. 在此方法中用于firstvisible项目。
I assumed that you use TextView inside R.layout.words to show the string, and I assumed the TextView id = myTextViewId. 我假定您在R.layout.words中使用TextView来显示字符串,并且假定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... 希望这个帮助...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.