0

为了让您更容易理解,我正在尝试制作一个应用程序,它向我显示书籍列表 - 书籍名称和封面图像。单击后,它将转到另一个活动,其中包含封面的放大图像和书的描述。它应该是可滚动的。
这是我有多少。

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>
4

2 回答 2

1
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
}
于 2013-10-16T15:09:34.317 回答
0
public void onListItemClick(ListView parent, View v, int position, long id) {
startActivity(new Intent(getBaseContext(), ActivityToStart.class).putExtra("book", items[position]));
}

然后在 ActivityToStart

  public class ActivityToStart  {
    private String bookName;
    @Override
      public void onCreate(Bundle bundleSavedInstance) {
        super.onCreate(bundleSavedInstance);
        getIntentExtras();
    }
    public void getIntentExtras(){
      this.bookName = getIntent().getExtra().getString("book");
    }


}
于 2013-10-16T17:19:45.993 回答