1

我正在开发一个用于列出歌曲的 Android 应用程序。单击任何歌曲(ListView 中的项目)时,其歌词(应显示文本文件)。当我单击 ListView 项目时,它会启动新活动,但不显示任何内容。我不确定是什么原因造成的。是因为我没有传递捆绑包或正确使用它们吗?问题是它不能通过 if 语句(在第二个活动中)来读取 txt 文件。任何帮助是极大的赞赏。

在此先感谢。这是代码

第一个活动

        public class ListOfSongs extends Activity {

        private ListView songsListView;
        private ArrayAdapter<String> listAdapter;

        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_list_of_songs);
            final String SONG_ONE = "songOne";
            final String SONG_TWO = "songTwo";
            // Find the ListView resource.
            songsListView = (ListView) findViewById(R.id.listofsongs);

            // Create and populate a List of songs names.
            final String[] songs = new String[] { "Song1", "Song2", "Song3",
                    "Song4", "Song5", "Song6", "Song7", "Song8" };
            ArrayList<String> songtList = new ArrayList<String>();
            songtList.addAll(Arrays.asList(songs));

            // Create ArrayAdapter using the songs list.
            // Each row in the ListView will be a TextView. The TextView is defined
            // in another file (res/layout/simplerow.xml).
            listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow,
                    songtList);
            // Set the ArrayAdapter as the ListView's adapter.
            songsListView.setAdapter(listAdapter);

            songsListView.setOnItemClickListener(new OnItemClickListener() {
                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {

                    Intent startReadingTheLyrics = new Intent(ListOfSongs.this,
                            ReadingTheLyrics.class);

                    if (position == 0) {
                        System.out.println("Position zero");
                        startReadingTheLyrics.putExtra("song1", SONG_ONE);
                        startActivity(startReadingTheLyrics);
                    } else if (position == 1) {
                        System.out.println("Position one");
                        startReadingTheLyrics.putExtra("song2", SONG_TWO);
                        startActivity(startReadingTheLyrics);
                    }

                }
            });
        }

第二个活动:

    public class ReadingTheLyrics extends Activity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.simplerow);

            Intent intent = getIntent();
            String song1 = intent.getStringExtra("song1");
            String song2 = intent.getStringExtra("song2");
    //It's not able to pass the if statement to be able to read the text file
            if (intent.hasExtra(song1)) {
                try {
                    // create an Input stream to read the file
                    InputStream songOne = getResources().openRawResource(
                            R.raw.songone);
                    // assign it to a string the method is down below
                    String lyricOne = inputStreamToString(songOne);
                    // get the TextView
                    TextView lyricTextView = (TextView) findViewById(R.id.rowTextView);
                    // set the text
                    lyricTextView.setText(lyricOne);

                } catch (IOException e) {
                    Log.e("DEBUG", "InputStreamToString failure");
                }

            }// end of reading song one

            if (intent.equals(song2)) {
                System.out.println("Chachawee mo bentna");
                try {
                    // create an Input stream to read the file
                    InputStream songTwo = getResources().openRawResource(
                            R.raw.songtwo);
                    // assign it to a string the method is down below
                    String lyricTwo = inputStreamToString(songTwo);
                    // get the TextView
                    TextView lyricTextView = (TextView) findViewById(R.id.rowTextView);
                    // set the text
                    lyricTextView.setText(lyricTwo);

                } catch (IOException e) {
                    Log.e("DEBUG", "InputStreamToString failure");
                }

            }// end of reading song one

        }// end of onCreate method

        private String inputStreamToString(InputStream is) throws IOException {
            // create a buffer
            StringBuffer sBuffer = new StringBuffer();
            DataInputStream dataIO = new DataInputStream(is);
            String strLine = null;

            while ((strLine = dataIO.readLine()) != null) {
                sBuffer.append(strLine + "\n");

            }
            dataIO.close();
            is.close();
            return sBuffer.toString();
        }
        }

activity_list_of_songs.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="@android:color/black"
    tools:context=".ListOfSongs" >

  <ListView android:layout_width="fill_parent"   
      android:layout_height="fill_parent" 
      android:textColor="@android:color/white"  
      android:textSize="25sp"
      android:id="@+id/listofsongs">  
    </ListView> 
</RelativeLayout>

简单行.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/rowTextView" 
 android:layout_width="fill_parent" 
 android:layout_height="wrap_content"
 android:padding="10dp"
 android:textSize="16sp" >
</TextView>
4

1 回答 1

0

if 语句在这里是不正确的。

  Intent intent = getIntent();
        String song1 = intent.getStringExtra("song1");
        String song2 = intent.getStringExtra("song2");
//It's not able to pass the if statement to be able to read the text file
        if (intent.hasExtra(song1)) 

当您这样做时intent.getStringExtra("song1")song1="SongOne"并且这不是您的 Intent 中可用的键,因此intent.hasExtra()返回 false 并且不满足 if 条件。
有关 Intent 的更多信息,请参阅此处: http: //developer.android.com/reference/android/content/Intent.html#hasExtra(java.lang.String)

于 2013-05-21T19:35:11.100 回答