Eric Declan Kots
Eric Declan Kots

Reputation: 3

Show text for each listview items

Can someone help me with this very simple question. I'm very+++ beginner.

This is my string for listview

String[] items = {"Silent night", "Deck The hall","Feliz navidad","Jingle bell", "song 4","song 5"};

Question:

How do I view lyric for each item in my listview? I store my lyric in res raw.

Details:

I have main textview and 4 buttons (Christmas, hillsong, button 3,button 4) in main.xml. When I click the Christmas button, it switches to the Christmas layout with Christmas songs list in the listview. When the title is clicked it refers back to main.xml and view the lyric in main textview.

Upvotes: 0

Views: 192

Answers (1)

bwoogie
bwoogie

Reputation: 4427

Check out http://www.vogella.de/articles/AndroidListView/article.html for some good examples on using listviews

Basically you'll create a new class that extends ListActivity.

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MyListActivity extends ListActivity {
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        String[] values = new String[] { "song1", "song2", "song3",
                "song4" };
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, values);
        setListAdapter(adapter);
    }

}

Then you'll need to check when the user clicks a list item and then pack it in and send it through an intent to your next activity with is lyrics.

@Override
        protected void onListItemClick(ListView l, View v, int position, long id) {
            String item = (String) getListAdapter().getItem(position);
            Intent intent = new Intent(this, Lyrics.class);
                    intent.putExtra("song", item); //item is the listitem ie. song1, etc.
                    startActivity(intent);
        }

Finally in the lyrics class you'll need to retrieve the extras.

@Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        Bundle extras = getIntent().getExtras();
        if(extras != null) {
        String song = extras.getString("song");
        }
...
}

Now you have the song in the lyrics activity and you can take it from there. I pretty much just wrote your entire app.... x_X

Upvotes: 1

Related Questions