我转到main.xml到Graphical Layout并将设计器拖到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?
感谢。
答案 0 :(得分:1)
您可以使用&#34;适配器&#34;可以是ArrayAdapte / BaseAdapter / CursorAdapter ,,,使用数组适配器的简单示例可能是
public class YourActivity extends Activity
{
private ListView lv;
public void onCreate(Bundle saveInstanceState) {
lv = (ListView) findViewById(R.id.your_list_view_id);
// Instanciating an array list (you don't need to do this, you already have yours)
ArrayList<String> your_array_list = new ArrayList<String>();
your_array_list.add("foo");
your_array_list.add("bar");
// This is the array adapter, it takes the context of the activity as a first // parameter, the type of list view as a second parameter and your array as a third parameter
ArrayAdapter<String> arrayAdapter =
new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, your_array_list);
lv.setAdapter(arrayAdapter);
setContentView(R.layout.your_layout);
}
}