简体   繁体   中英

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

I try to make app with RecyclerView where you see the details of item when clicked. 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 {

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); which is likely causing a null pointer exception when you try to call getText() on it.

Uncomment that line and then also uncomment this line in your onBindViewHolder: viewHolder.itemTitle2.setText(titles2[i]); .

If you get a new error let me know and be sure to include a stacktrace for us.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM