[英]Accessing listview items without onclicklistener in android
我正在使用這個Android應用程序,在該應用程序中,我得到了語音記錄,以便在列表視圖中填充與所識別單詞的可能匹配項。
現在,我想訪問列表視圖的第一個元素,但是我想在列表視圖填充后立即獲取此值,而不是在選擇項目(或用戶的任何其他活動)時獲取此值。 我想使它自動化。 基本上,我希望系統盡快識別出已識別的單詞。
我了解我需要創建一個存儲已識別單詞的值的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
字符串,也無法從中創建吐司消息。
請幫助解決此問題。
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>
我希望這有幫助。 我真的被困住了。
您可以使用listView.setOnScrollListener(this);
這給你listview第一項,
你必須implements OnScrollListener
的使用方法
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {}
在此方法中用於firstvisible項目。
我假定您在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...");
希望這個幫助...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.