为了让您更容易理解,我正在尝试制作一个应用程序,它可以向我显示书籍列表 - 书名和封面图像。单击后,它将转到另一个 Activity ,其中包含封面的放大图像和书籍的说明。它应该是可滚动的。
这就是我所拥有的。
MainActivity.java
public class MainActivity extends ListActivity {
TextView select;
String[] items = { "Naruto", "One Piece", "Bleach", "Harry Potter", "Vampire's Assistant",
"Pet Cemetery" };
/** Called with the activity is first created. */
@Override
public void onCreate(Bundle a) {
super.onCreate(a);
setContentView(R.layout.activity_main);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, items));
select = (TextView) findViewById(R.id.selection);
}
public void onListItemClick(ListView parent, View v, int position, long id) {
select.setText(items[position]);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/selection"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false"/>
</LinearLayout>
最佳答案
public void onListItemClick(ListView parent, View v, int position, long id) {
Intent i = new Intent(this, ActivityTwo.class);
i.putExtra("book_position", position);
startActivity(i);
}
ActivityTwo(在 onCreate 中或任何您觉得舒服的地方):
Bundle extras = getIntent().getExtras();
if (extras != null && extras.getInt("book_position", 0) > 0) {
int bookPosition = extras.getInt("book_position");
// Do something with the position. E.g. retrieve the data from the String array in this position and populate a TextView/ImageView
}
关于java - 通过单击 ListViewItem 打开另一个 Activity 并传递文本和图像。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19406780/