我想使用class recyclerview adapter将一些细节数据从我的模型传递到另一个活动。
适配器的代码:
public class Tab1RecyclerAdapter extends
RecyclerView.Adapter<Tab1RecyclerAdapter.MyViewHolder> {
private Context context;
private List<Tab1BookModel> bookList;
private LayoutInflater inflater;
public Tab1RecyclerAdapter(Context context, List<Tab1BookModel> bookList) {
inflater = LayoutInflater.from(context);
this.context = context;
this.bookList = bookList;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.book_list_item, parent, false);
MyViewHolder holder = new MyViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Tab1BookModel book = bookList.get(position);
holder.setData(book, position);
}
@Override
public int getItemCount() {
return bookList.size();
}
class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private ImageView downloadIcon;
private TextView bookTitle, bookAuthor;
private View container;
private int position;
private Tab1BookModel book;
MyViewHolder(View view) {
super(view);
itemView.setOnClickListener(this);
bookTitle = (TextView) view.findViewById(R.id.book_title);
bookAuthor = (TextView) view.findViewById(R.id.book_author);
downloadIcon = (ImageView) view.findViewById(R.id.download_icon);
// downloadIcon.setOnClickListener(this);
container = view.findViewById(R.id.container_book_list_item);
container.setOnClickListener(this);
}
void setData(Tab1BookModel book, int position) {
this.bookTitle.setText(book.getTab1BookTitle());
this.bookAuthor.setText(book.getTab1BookAuthor());
this.position = position;
this.book = book;
}
OnClick方法:
@Override
public void onClick(View view)
{
int position = getAdapterPosition();
if (position != RecyclerView.NO_POSITION)
{
Intent intent = new Intent(context, BookReaderActivity.class);
intent.putExtra("content", bookList.get(position).getTab1BookContent());
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
}
}
}
型号代码:
public class Tab1BookModel {
private int bookTitle;
private int bookAuthor;
private int bookContent;
public int getTab1BookTitle() {
return bookTitle;
}
public void setBookTitle(int bookTitle) {
this.bookTitle = bookTitle;
}
public int getTab1BookAuthor() {
return bookAuthor;
}
public void setBookAuthor(int bookAuthor) {
this.bookAuthor = bookAuthor;
}
public int getTab1BookContent() {
return bookContent;
}
public void setBookContent(int bookContent) {
this.bookContent = bookContent;
}
public static List<Tab1BookModel> getObjectList() {
List<Tab1BookModel> dataList = new ArrayList<>();
int[] title = getTitle();
int[] author = getAuthor();
int[] content = getContent();
for (int i = 0; i < title.length; i++) {
Tab1BookModel books = new Tab1BookModel();
books.setBookTitle(title[i]);
books.setBookAuthor(author[i]);
books.setBookContent(content[i]);
dataList.add(books);
}
return dataList;
}
private static int[] getTitle() {
int[] title = {
R.string.title_1, R.string.title_2, R.stringtitle_3,
R.string.title_4, R.string.title_5, R.string.title_6,
R.string.title_7, R.string.title_8, R.string.title_9
};
return title;
}
private static int[] getAuthor() {
int[] author = {
R.string.author_1, R.string.author_2, R.string.author_3,
R.string.author_4, R.string.author_5, R.string.author_6,
R.string.author_7, R.string.author_8, R.string.author_9
};
return author;
}
我需要将一个项目传递到另一个项目的数据列表:
private static int[] getContent() {
int[] content = {
R.string.book_1, R.string.book_2, R.string.book_3,
R.string.book_4, R.string.book_5, R.string.book_6,
R.string.book_7, R.string.book_8, R.string.book_9
};
return content;
}
}
活动,我想在其中传递一些数据:
public class BookReaderActivity extends AppCompatActivity {
TextView bookContent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_book_reader);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
bookContent = (TextView) findViewById(R.id.book_content);
bookContent.setText(getIntent().getExtras().getString("content"));
}
}
答案 0 :(得分:0)
尝试登录OnClick方法以确认它已被调用,然后在您的Activity中,您可以返回数据
getIntent.getIntExtra("content");
答案 1 :(得分:0)
由于您传递了String资源ID,因此您需要首先获取具有资源ID的String,如下所示:
String stringBookContent = getResources().getString(getIntent().getIntExtra("content", 0));
bookContent.setText(stringBookContent);
将它添加到BookReaderActivity类。