title: Reading text files when item is clicked in ListView- Android
body: <p>I'm deveoping an Android app for listing songs. When any song( item in the ListView) is clicked, its lyrics( a Text File should be displayed). When I click on the ListView item, it starts the new activity but displays nothing. I'm not sure what's causing this. Is it because I'm not passing the bundles or using them correctly? The problem is it can't pass the if statement( in the second activity) to read the txt file. Any help is greatly appreciated.</p>
  
  <p>Thanks in advance.Here's the code</p>
  
  <p>First Activity</p>
  
  <pre><code>        public class ListOfSongs extends Activity {
  
          private ListView songsListView;
          private ArrayAdapter&lt;String&gt; 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&lt;String&gt; songtList = new ArrayList&lt;String&gt;();
              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&lt;String&gt;(this, R.layout.simplerow,
                      songtList);
              // Set the ArrayAdapter as the ListView's adapter.
              songsListView.setAdapter(listAdapter);
  
              songsListView.setOnItemClickListener(new OnItemClickListener() {
                  public void onItemClick(AdapterView&lt;?&gt; 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);
                      }
  
                  }
              });
          }
  </code></pre>
  
  <p>Second Activity:</p>
  
  <pre><code>    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();
          }
          }
  </code></pre>
  
  <p>activity_list_of_songs.xml</p>
  
  <pre><code>    &lt;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" &gt;
  
    &lt;ListView android:layout_width="fill_parent"   
        android:layout_height="fill_parent" 
        android:textColor="@android:color/white"  
        android:textSize="25sp"
        android:id="@+id/listofsongs"&gt;  
      &lt;/ListView&gt; 
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>simplerow.xml</p>
  
  <pre><code>&lt;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" &gt;
  &lt;/TextView&gt;
  </code></pre>