2013-05-21 6 views
1

나는 노래 목록 작성을 위해 Android 앱을 개발 중입니다. 모든 노래 (ListView의 항목)를 클릭하면 가사가 표시됩니다 (텍스트 파일이 표시되어야 함). ListView 항목을 클릭하면 새 활동이 시작되지만 아무것도 표시되지 않습니다. 나는 이것이 무엇을 일으키는 지 모르겠습니다. 번들을 넘기거나 올바르게 사용하지 않기 때문입니까? 문제는 두 번째 활동에서 if 문을 전달하여 txt 파일을 읽을 수 없다는 것입니다. 어떤 도움이라도 대단히 감사합니다.ListView에서 항목을 클릭하면 텍스트 파일을 읽음 - Android

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> 

simplerow.xml : advance.Here의 코드

첫 번째 활동

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

       } 
      }); 
     } 

두 번째 활동에서

감사합니다

<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> 
+0

.xml을 게시 할 수 있습니까? –

+0

방금 ​​두 개의 XML 파일을 게시했습니다. 감사합니다 – EverGreen

답변

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.hasExtra() false를 반환하고, 만약 조건이 만족되지 않습니다.
인 텐트에 대한 자세한 내용은 여기를 참조하십시오. http://developer.android.com/reference/android/content/Intent.html#hasExtra(java.lang.String)

+0

감사합니다, nikhil.thakkar. 문제 해결됨. – EverGreen

+0

당신은 오신 것을 환영합니다. –

관련 문제