0

我将 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?

谢谢。

4

1 回答 1

1

您可以将“适配器”用于可以是 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);

    }
}
于 2012-06-04T06:44:42.177 回答