0

I went to the main.xml to the Graphical Layout and dragged over to the designer a ExpandableList . I can see it also in the main.xml runtime code:

<ExpandableListView
        android:id="@+id/expandableListView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ExpandableListView>

But when im running my application i dont see it in the android device. What i want to do is to add arrayList string to this ListView so i can select from this list in my device.

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);
    }
}

I have a Log.i and then under it ihave itemsList wich is ArrayList Im getting a list of available languages in my Device. I want to add this list to the ListView so i can select from there the language in my device and it will change the language in real time.

The problem is that i dont see the ListView at all when running the application on my device and second problem is how to add the list to the ListView ?

Thanks.

1
  • It's very basic question. You can find many tutorials about this. Commented Jun 4, 2012 at 6:59

1 Answer 1

1

You can use "Adapters" for that which can be ArrayAdapte/BaseAdapter/CursorAdapter ,,,a simple example using array adapter could be

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);

    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.