java - 通过单击 ListViewItem 打开另一个 Activity 并传递文本和图像。

标签 java android listview

为了让您更容易理解,我正在尝试制作一个应用程序,它可以向我显示书籍列表 - 书名和封面图像。单击后,它将转到另一个 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/

相关文章:

java - 导致给定后缀数组的不同字符的最小数量

java - test !"".equals(someString) 的值是多少

java - 如何以编程方式正确设置RelativeLayout的设置?

android - 使用 setRepeating() 方法知道下一个闹钟时间

wpf - 使用列表框拖动选择

带有复选框删除/获取行 ID 的 Android ListView

java - Android 中使用 SharedPreferences 的动态列表

java - 获取当前动态web项目JAVA的路径

java - Android中的Itext段落对齐问题

java - 添加一个 jar 到 maven exec :java classpath