如何在 recyclerview 中包含不同的意图 [英] how to include different intent in recyclerview

查看:25
本文介绍了如何在 recyclerview 中包含不同的意图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我计划在我的项目中包含一个 CardView.我已经在我的项目中包含了 RecyclerView 和卡片视图.问题是,我想为每张卡调用不同的活动.我对每张卡都实施了不同的意图.但它需要我初始化数据.这是我的原始代码:

I plan to include a CardView in my project. i have already included RecyclerView and card view in my project. the problem is, i want to call for different activity for each card. i have implement different intent for each card. but it require me to initialize the data. this is my original code:

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {

private Context context;

private String[] titles = {"Add new Research",
        "View Your Research"};

private String[] details = {"Add your research files here",
        "View all of your posted research"};

private int[] images = { R.drawable.add,
        R.drawable.view};

class ViewHolder extends RecyclerView.ViewHolder{

    public int currentItem;
    public ImageView itemImage;
    public TextView itemTitle;
    public TextView itemDetail;

    public ViewHolder(View itemView) {
        super(itemView);
        itemImage = (ImageView)itemView.findViewById(R.id.item_image);
        itemTitle = (TextView)itemView.findViewById(R.id.item_title);
        itemDetail =
                (TextView)itemView.findViewById(R.id.item_detail);

        itemView.setOnClickListener(new View.OnClickListener() {
            @Override public void onClick(View v) {
                int position = getAdapterPosition();
                Intent intent = new Intent(context, Choose.class);

                if(position==0){
                   intent =  new Intent(context, AddFiles.class);

                }else if(position==1){
                    intent =  new Intent(context, ViewFiles.class);

                }
                context.startActivity(intent);
            }
        });
    }
}

当我点击卡片视图时,它表示我的程序没有响应.

when i click on the card view, it stated that my program are not responding.

即使我将意图初始化为

even if i initialize intent as

Intent intent = null;
 if(position==0){
  intent =  new Intent(context, AddFiles.class);

   }else if(position==1){
    intent =  new Intent(context, ViewFiles.class);

     }
     context.startActivity(intent);

还是有错误,我该怎么办?或者有更好的方法来做到这一点.这是我的 logcat 错误.

there still an error, what should i do? or is there better way to do it. this is my logcat error.

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.user.mcormpelo, PID: 3645
              java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
                  at android.content.ComponentName.<init>(ComponentName.java:128)
                  at android.content.Intent.<init>(Intent.java:4449)
                  at com.example.user.mcormpelo.RecyclerAdapter$ViewHolder$1.onClick(RecyclerAdapter.java:46)
                  at android.view.View.performClick(View.java:5198)
                  at android.view.View$PerformClick.run(View.java:21147)
                  at android.os.Handler.handleCallback(Handler.java:739)
                  at android.os.Handler.dispatchMessage(Handler.java:95)
                  at android.os.Looper.loop(Looper.java:148)
                  at android.app.ActivityThread.main(ActivityThread.java:5417)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

推荐答案

您没有初始化 context

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {

        private Context context;

        public RecyclerAdapter(Context context) {
            this.context = context;
        }

传递来自调用方活动的 Activity 引用.示例用法

Pass the Activity reference from the caller activity. sample usage

  RecyclerAdapter rcAdapter = new RecyclerAdapter(MainActivity.this);

这篇关于如何在 recyclerview 中包含不同的意图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆