Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
123 views
in Technique[技术] by (71.8m points)

java - How can I make multiple activities be in one activity changing only strings?

I have an app that contains a ListView in its menu which I want to link to one activity with the same layout style but the content inside should be different after being clicked on.

The ListView contains a list of songs, and the layout of an individual item when clicked contains a Title (TextView), Lyrics (TextView), a "next_button" button, a "previous_button" button to move between songs, a "back_button" button to get back to the ListView and a "play_tune" button which plays audio for that specific song. Not all of the songs contain audio, but where the "play_tune" button lies, I would like the app icon ic_launcher-web.png showing there instead.

The XML file I would like to show (for a song with audio) is shown below:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/song"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".ScoutSongs" >

<TextView
    android:id="@+id/Title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:textColor="#FFFFFF"
    android:textSize="20sp" />

<ImageButton
    android:id="@+id/play_tune"
    android:src="@drawable/play_tune"
    android:contentDescription="@string/play_tune"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/Title"
    android:layout_alignTop="@+id/Title" />

<ScrollView
    android:id="@+id/LyricsView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/back_button"
    android:layout_alignLeft="@+id/Title"
    android:layout_alignRight="@+id/play_tune"
    android:layout_below="@+id/play_tune" >

    <TextView
        android:id="@+id/Lyrics"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#FFFFFF" />

</ScrollView>

<Button
    android:id="@+id/back_button"
    android:layout_width="65dp"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignRight="@+id/LyricsView"
    android:text="@string/back"
    android:textColor="#FFFFFF" />

<Button
    android:id="@+id/previous_button"
    android:layout_width="95dp"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/back_button"
    android:layout_alignBottom="@+id/back_button"
    android:layout_alignLeft="@+id/LyricsView"
    android:text="@string/previous"
    android:textColor="#FFFFFF" />

<Button
    android:id="@+id/next_button"
    android:layout_width="95dp"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/previous_button"
    android:layout_alignBottom="@+id/previous_button"
    android:layout_toRightOf="@+id/previous_button"
    android:text="@string/next"
    android:textColor="#FFFFFF" />

My current SongActivity.java file is shown below:

package com.lmarshall1995.scoutsongs;

import com.lmarshall1995.scoutsongs.R;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;

public class SongActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.song);
        Intent i = getIntent();
        String Title = i.getStringExtra("Title");
        String Lyrics = i.getStringExtra("Lyrics");
        String TuneToast = i.getStringExtra("TuneToast");
        String Tune = i.getStringExtra("Tune");
        setupNavigationButton();

        Toast toast = Toast.makeText(this, TuneToast , Toast.LENGTH_LONG);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();

        final Button back = (Button) findViewById(R.id.back_button);
        back.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                finish();
            }
        });

        final Button previous = (Button) findViewById(R.id.previous_button);
        previous.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent previousIntent = new Intent(SongActivity.this, SongActivity.class);
                SongActivity.this.startActivity(previousIntent);
                finish();
            }
        });

        final Button next = (Button) findViewById(R.id.next_button);
        next.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent nextIntent = new Intent(SongActivity.this, SongActivity.class);
                SongActivity.this.startActivity(nextIntent);
                finish();
            }
        });

        final ImageButton play_tune = (ImageButton) findViewById(R.id.play_tune);
        play_tune.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                ImageButton play_tune = (ImageButton) findViewById(R.id.play_tune);
                play_tune.setOnClickListener(new View.OnClickListener() {
                    MediaPlayer mp = MediaPlayer.create(SongActivity.this, Tune);
                    public void onClick(View arg0) {
                        if (mp.isPlaying()) {
                            mp.stop();
                            mp.prepareAsync();
                            mp.seekTo(0);
                        } else {
                            mp.start();
                        }
                    }
                });
            }
        });

    }
    private void setupNavigationButton() {}
}

And my Menu.java for my ListView is here:

package com.lmarshall1995.scoutsongs;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ListView;

public class Menu extends ListActivity{

    String classes[] = {"Song_AliceTheCamel_Activity", "Song_Bingo_Activity",
            "Song_CampfiresBurning_Activity", "Song_DownAtTheStation_Activity",
            "Song_EverywhereWeGo_Activity", "Song_FeFi_Activity",
            "Song_GingGangGooly_Activity", "Song_HeadShouldersKneesToes_Activity",
            "Song_IfYoureHappyAndYouKnowIt_Activity", "Song_JoesButtonFactory_Activity",
            "Song_Kumbaya_Activity", "Song_LittleGreenFrog_Activity",
            "Song_Meatball_Activity", "Song_NationalAnthem_Activity",
            "Song_OldMacDonald_Activity", "Song_PizzaHut_Activity",
            "Song_QuartermastersStores_Activity", "Song_RowRowRowYourBoat_Activity",
            "Song_ShineUpYourButtons_Activity", "Song_ThreeBlindJellyfish_Activity",
            "Song_Underwear_Activity", "Song_Valerie_Activity", "Song_Worms_Activity",
            "Song_XCommissionersInTheTown_Activity", "Song_YogiBear_Activity",
            "Song_ZipADeeDooDah_Activity"};

    String items[] = {"Alice The Camel", "Bingo", "Campfire's Burning", "Down At The Station",
            "Everywhere We Go", "Fe Fi", "Ging Gang Gooly", "Head, Shoulders, Knees & Toes",
            "If You're Happy And You Know It", "Joe's Button Factory", "Kumbaya",
            "Little Green Frog", "Meatball", "National Anthem (UK)", "Old MacDonald", "Pizza Hut",
            "Quartermaster's Stores", "Row, Row, Row Your Boat", "Shine Up Your Buttons",
            "Three Blind Jellyfish", "Underwear", "Valerie", "Worms",
            "X-Commissioner's In The Town", "Yogi Bear", "Zip A Dee Doo Dah"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.menu);

        final ImageButton settings = (ImageButton) findViewById(R.id.settings_button);
        settings.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent settingsIntent = new Intent(Menu.this, Settings.class);
                Menu.this.startActivity(settingsIntent);
            }
        });

        final ImageButton back = (ImageButton) findViewById(R.id.exit_button);
        back.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                finish();
            }
        });

        setListAdapter(new ArrayAdapter<String>(Menu.this, android.R.layout.simple_list_item_1, items));
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        Intent i = new Intent(Menu.this, SongActivity.class);
        i.putExtra("Title", "Song_" + classes + "_Title");
        i.putExtra("Lyrics", "Song_" + classes + "_Lyrics");
        i.putExtra("TuneToast", "To be sung in the tune of 
" + classes);
        startActivity(i);
    }

}

All of my Titles and Lyrics are currently in my Valuesstrings.xml file.

How can I make all my songs be in one "SongActivity" class after being selected in ListView? I believe that if you have to write anything out more than once, there should be an easier way to do it.

Edit: How can I make MediaPlayer mp = MediaPlayer.create(SongActivity.this, Tune); and String Tune = i.getStringExtra("Tune"); work together?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

When selecting a list item, call your SongActivity with extras in the intent, such as SongName and other params you need. Then read it and populate the fields accordingly.

onItemSelected method:

Intent i = new Intent(this, SongActivity.class);
i.putExtra("songName", "Song name here"); //get song name from the item selected
....
startActivity(i);

In your SongActivity class:

onCreate(...){
    Intent i = getIntent();
    String songName = i.getStringExtra("songName");
    ...
}

Edit: this way all other fixed text can be included in SongActivity, just send the song to that activity

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    String song = l.getItemAtPosition(position).toString(); //this is the selected song
    Intent i = new Intent(Menu.this, SongActivity.class);
    i.putExtra("Song", song);
    startActivity(i);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...