简体   繁体   English

如何在RecyclerView中使用不同的数据和照片在项目单击上打开新的活动

[英]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.

相关问题 单击RecyclerView中包含在片段中的项目以打开带有传递数据的新活动 - Click On An Item In RecyclerView Contained In A Fragment To Open New Activity With Passed Data 如何在 recyclerView 项目上打开不同的活动 onclick - How to open a different activity on recyclerView item onclick 如何在传递单击的自定义列表项的数据时单击带有RecyclerView的片段项时打开新活动? - How to open a new activity when an item of a fragment with RecyclerView is clicked while passing the data of the clicked custom list item? 在 RecyclerView 的新活动中打开项目(来自 json 的数据) - Open item in new activity from RecyclerView (data from json) 如何将数据从recyclerview中的单击项传递到ViewModel并打开新活动? - How to pass data from clicked item in recyclerview to viewmodel and open new activity? 如何根据RecyclerView中的单击项在新活动中打开特定片段 - How to open specific Fragment in new Activity depending on clicked item in RecyclerView 在Firebase RecyclerView中单击项目时如何打开新活动 - How to open a new activity when an item is clicked in Firebase RecyclerView 如何通过单击来自recyclerview的项目来打开新活动 - How to open new activity from clicking an item from recyclerview 如何在 Recyclerview 项目单击方法中将图像获取到新活动 - How to Get image to new Activity in Recyclerview item click method 带有单击项上的导航栏的recyclerview打开活动 - recyclerview open activity with navbar on click item
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM