我正在推出一個用於列出歌曲的Android應用程序。當任何歌曲(ListView中的項目)被點擊時,其歌詞(應顯示文本文件)。當我點擊ListView項目時,它開始新的活動,但什麼都不顯示。我不確定是什麼原因造成的。是因爲我沒有通過捆綁或正確使用它們?問題是它不能通過if語句(在第二個活動中)來讀取txt文件。任何幫助是極大的讚賞。在ListView中單擊項目時讀取文本文件Android
感謝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);
}
}
});
}
次活動:
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
<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>
您可以發佈您的.xml嗎? –
我剛剛發佈了兩個xml文件。謝謝 – EverGreen