写一个listview适配器,点击适配器里面的按钮跳转到一个新的activity,并显示书籍的详细信息
时间: 2024-05-09 14:18:51 浏览: 78
以下是一个简单的listview适配器示例,点击适配器中的按钮会跳转到一个新的activity,显示书籍的详细信息。
```java
public class BookAdapter extends BaseAdapter {
private Context mContext;
private List<Book> mBookList;
public BookAdapter(Context context, List<Book> bookList) {
mContext = context;
mBookList = bookList;
}
@Override
public int getCount() {
return mBookList.size();
}
@Override
public Object getItem(int position) {
return mBookList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(R.layout.item_book, parent, false);
}
TextView titleTextView = convertView.findViewById(R.id.titleTextView);
TextView authorTextView = convertView.findViewById(R.id.authorTextView);
Button detailButton = convertView.findViewById(R.id.detailButton);
final Book book = mBookList.get(position);
titleTextView.setText(book.getTitle());
authorTextView.setText(book.getAuthor());
detailButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(mContext, BookDetailActivity.class);
intent.putExtra("book", book);
mContext.startActivity(intent);
}
});
return convertView;
}
}
```
在上面的示例中,我们使用了自定义的`Book`对象来表示一个书籍,`item_book.xml`布局文件如下所示:
```xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp">
<LinearLayout
android:orientation="vertical"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content">
<TextView
android:id="@+id/titleTextView"
android:textSize="18sp"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/authorTextView"
android:textSize="16sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<Button
android:id="@+id/detailButton"
android:text="详情"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
```
在`getView()`方法中,我们为每个`detailButton`按钮设置了一个点击监听器。当用户点击按钮时,我们会创建一个新的`Intent`对象,并将当前书籍对象作为额外的数据传递到`BookDetailActivity`中。在`BookDetailActivity`中,我们可以通过`getIntent().getExtras().getParcelable("book")`方法来获取当前书籍对象。
```java
public class BookDetailActivity extends AppCompatActivity {
private TextView mTitleTextView;
private TextView mAuthorTextView;
private TextView mDescriptionTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_book_detail);
mTitleTextView = findViewById(R.id.titleTextView);
mAuthorTextView = findViewById(R.id.authorTextView);
mDescriptionTextView = findViewById(R.id.descriptionTextView);
Book book = getIntent().getExtras().getParcelable("book");
mTitleTextView.setText(book.getTitle());
mAuthorTextView.setText(book.getAuthor());
mDescriptionTextView.setText(book.getDescription());
}
}
```
`activity_book_detail.xml`布局文件如下所示:
```xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:padding="16dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/titleTextView"
android:textSize="24sp"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/authorTextView"
android:textSize="18sp"
android:layout_marginTop="8dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/descriptionTextView"
android:textSize="16sp"
android:layout_marginTop="16dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
```
阅读全文