在ListView中单击项目时读取文本文件-Android
我正在开发一个Android应用程序来列出歌曲。单击任何歌曲(ListView中的项目)时,其歌词(应显示文本文件)。当我单击ListView项目时,它会启动新活动,但不显示任何内容。我不确定是什么原因造成的。是因为我没有传递包或正确使用包吗?问题是它无法传递if语句(在第二个活动中)来读取txt文件。非常感谢您的帮助 提前谢谢。这是密码 第一项活动在ListView中单击项目时读取文本文件-Android,android,Android,我正在开发一个Android应用程序来列出歌曲。单击任何歌曲(ListView中的项目)时,其歌词(应显示文本文件)。当我单击ListView项目时,它会启动新活动,但不显示任何内容。我不确定是什么原因造成的。是因为我没有传递包或正确使用包吗?问题是它无法传递if语句(在第二个活动中)来读取txt文件。非常感谢您的帮助 提前谢谢。这是密码 第一项活动 public class ListOfSongs extends Activity { private List
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);
}
}
});
}
活动\u歌曲列表\u.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>
此处的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条件不满足。有关意图的更多信息,请参阅此处:您可以发布您的.xml吗?我刚刚发布了两个xml文件。谢谢你,nikhil.thakkar。问题解决了。
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))