我将 main.xml 转到图形布局,然后将 ExpandableList 拖到设计器上。我也可以在 main.xml 运行时代码中看到它:
<ExpandableListView
android:id="@+id/expandableListView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ExpandableListView>
但是当我运行我的应用程序时,我在 android 设备中看不到它。我想要做的是将 arrayList 字符串添加到这个 ListView 中,这样我就可以从我的设备中的这个列表中进行选择。
package com.testotspeech;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Locale;
import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
public class AndroidTestToSpeechActivity extends Activity implements
TextToSpeech.OnInitListener {
/** Called when the activity is first created. */
private TextToSpeech tts;
private Button btnSpeak;
private EditText txtText;
private ListView lview;
private ArrayList<String> itemsList;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
itemsList = new ArrayList<String>();
Log.i("----------",Arrays.toString(Locale.getAvailableLocales()));
itemsList.add(Arrays.toString(Locale.getAvailableLocales()));
tts = new TextToSpeech(this, this);
btnSpeak = (Button) findViewById(R.id.btnSpeak);
lview = (ListView) findViewById(R.id.expandableListView1);
txtText = (EditText) findViewById(R.id.txtText);
// button on click event
btnSpeak.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
speakOut();
}
});
}
@Override
public void onDestroy() {
// Don't forget to shutdown tts!
if (tts != null) {
tts.stop();
tts.shutdown();
}
super.onDestroy();
}
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
int result = tts.setLanguage(Locale.ENGLISH);
if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e("TTS", "This Language is not supported");
} else {
btnSpeak.setEnabled(true);
speakOut();
}
} else {
Log.e("TTS", "Initilization Failed!");
}
}
private void speakOut() {
String text = txtText.getText().toString();
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}
}
我有一个 Log.i,然后在它下面有一个 itemsList,它是 ArrayList 我在我的设备中获取可用语言的列表。我想将此列表添加到 ListView,以便我可以从那里选择设备中的语言,它会实时更改语言。
问题是在我的设备上运行应用程序时我根本看不到 ListView,第二个问题是如何将列表添加到 ListView?
谢谢。