[英]How to open new Activity on item click with different data and photo in RecyclerView
EDIT I( moved onClick to onBindViewHolder but still no success 编辑我(将onClick移至onBindViewHolder,但仍未成功
I try to make app with RecyclerView where you see the details of item when clicked. 我尝试使用RecyclerView制作应用程序,您可以在其中单击时查看项目的详细信息。 So you have a list of items with photo and its title. 因此,您具有带有照片及其标题的物品清单。 when you click on it, new screen is displayed with new/different photo, the same title and additional text. 当您单击它时,将显示新屏幕,其中包含新的/不同的照片,相同的标题和其他文本。 The app design is as on picture 应用程序设计如图所示
The code of my Adapter is : 我的适配器的代码是:
public class RecyclerAdapter extends recyclerView.Adapter { 公共类RecyclerAdapter扩展了recyclerView.Adapter {
private String[] titles = {
"Title A",
"Title B",
"Title C"};
private String[] description = {
"Description A",
"Description B",
"Description C"};
private int[] images = {
R.drawable.picture A,
R.drawable.picture B,
R.drawable.picture C,};
private int[] images2 = {
R.drawable.picture A2,
R.drawable.picture B2,
R.drawable.picture C2,
};
class ViewHolder extends RecyclerView.ViewHolder{
public ImageView itemImage;
public ImageView itemImage2;
public TextView itemTitle;
public TextView itemTitle2;
public ViewHolder(View itemView) {
super(itemView);
itemImage = (ImageView)itemView.findViewById(R.id.image_start);
itemTitle = (TextView)itemView.findViewById(R.id.article_title);
//SecondActivity where different picture and decription should be displayed, but when I uncomment it the app crashes
//itemTitle2 =(TextView)itemView.findViewById(R.id.article_ingredients);
//itemImage = (ImageView)itemView.findViewById(R.id.image_details);
itemView.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
int position = getAdapterPosition();
Intent intent = new Intent(v.getContext(), SecondActivity.class);
intent.putExtra("title",itemTitle.getText().toString());
intent.putExtra("description", itemTitle2.getText().toString()); //this one on makes app crash
v.getContext().startActivity(intent);
}
});
}
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.article_layout, viewGroup, false);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
viewHolder.itemTitle.setText(titles[i]);
// viewHolder.itemDetail.setText(details[i]);
viewHolder.itemImage.setImageResource(images[i]);
// viewHolder.itemTitle2.setText(titles2[i]);
}
@Override
public int getItemCount() {
return titles.length;
}
}
I was trying different options but without results. 我尝试了其他选择,但没有结果。 Thanks in advance for assistance 在此先感谢您的协助
You have commented out itemTitle2 =(TextView)itemView.findViewById(R.id.article_ingredients);
您已注释掉itemTitle2 =(TextView)itemView.findViewById(R.id.article_ingredients);
which is likely causing a null pointer exception when you try to call getText()
on it. 当您尝试在其上调用getText()
时,可能会导致空指针异常。
Uncomment that line and then also uncomment this line in your onBindViewHolder: viewHolder.itemTitle2.setText(titles2[i]);
取消注释该行,然后在您的onBindViewHolder中取消注释此行: viewHolder.itemTitle2.setText(titles2[i]);
. 。
If you get a new error let me know and be sure to include a stacktrace for us. 如果您遇到新的错误,请告诉我,并确保为我们包括一个堆栈跟踪。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.