从RecyclerView

时间:2017-03-26 16:20:33

标签: android android-recyclerview

我正在使用RecycleView,当我点击项目

时,我的应用程序崩溃了
public class ContactAdapter extends RecyclerView.Adapter<ContactAdapter.ContactViewHolder> {

    private Context mContext;
    private List<ContactInfo> contactList;
    private static OnItemClickListener ItemClickListener;

    public ContactAdapter(Context context , List<ContactInfo> contactList) {
        this.contactList = contactList;
        this.mContext = context;
    }


    @Override
    public int getItemCount() {
        return contactList.size();
    }

    @Override
    public void onBindViewHolder(ContactViewHolder contactViewHolder, int i) {
       final ContactInfo ci = contactList.get(i);
        contactViewHolder.vName.setText(ci.name);
        contactViewHolder.vSurname.setText(ci.surname);
        contactViewHolder.vEmail.setText(ci.title);
        contactViewHolder.vTitle.setText(ci.email);

    }

    @Override
    public ContactViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
            View itemView = LayoutInflater.
                    from(viewGroup.getContext()).
                    inflate(R.layout.cardview, viewGroup, false);

            return new ContactViewHolder(itemView);
    }



    public static class ContactViewHolder extends RecyclerView.ViewHolder  implements View.OnClickListener, View.OnLongClickListener{

        protected TextView vName;
        protected TextView vSurname;
        protected TextView vEmail;
        protected TextView vTitle;
        private OnItemClickListener clickListener;

        public ContactViewHolder(View v) {
            super(v);
            vName =  (TextView) v.findViewById(R.id.txtName);
            vSurname = (TextView)  v.findViewById(R.id.txtSurname);
            vEmail = (TextView)  v.findViewById(R.id.txtEmail);
            vTitle = (TextView) v.findViewById(R.id.title);

            v.setTag(v);
            v.setOnClickListener(this);
            v.setOnLongClickListener(this);
        }

        public void setClickListener(OnItemClickListener itemClickListener) {
            this.clickListener = itemClickListener;
        }
        @Override
        public void onClick(View view) {
            clickListener.onClick(view, getLayoutPosition(),false);
        }

        @Override
        public boolean onLongClick(View view) {
            clickListener.onClick(view, getLayoutPosition() , true);
            return true;
        }
    }



    public interface OnItemClickListener {
        void onClick(View view, int layoutPosition, boolean b);
    }


    public void SetOnItemClickListener(final OnItemClickListener mItemClickListener) {
        this.ItemClickListener = mItemClickListener;
    }
}

以下是我的MainActivity,当我尝试使用SetOnItemClickListener时,它会崩溃

   final ContactAdapter ca = new ContactAdapter(getApplicationContext(),createList(30));

        recList.setAdapter(ca);

        ca.SetOnItemClickListener(new ContactAdapter.OnItemClickListener() {
            @Override
            public void onClick(View view, int layoutPosition, boolean b) {

                Intent intent = new Intent("com.example.as.a.aa_Detail");
                startActivity(intent);
            }
        });
    }

2 个答案:

答案 0 :(得分:1)

我认为您尚未在viewhHolder中初始化clickListener。 clicklistener为null并导致崩溃。您需要通过调用相应的set方法来设置其值。

答案 1 :(得分:0)

更新您的ContactViewHolder,如下所示:

public static class ContactViewHolder extends RecyclerView.ViewHolder  implements View.OnClickListener, View.OnLongClickListener 
{
    protected TextView vName;
    protected TextView vSurname;
    protected TextView vEmail;
    protected TextView vTitle;

    public ContactViewHolder(View v) {
        super(v);
        vName =  (TextView) v.findViewById(R.id.txtName);
        vSurname = (TextView)  v.findViewById(R.id.txtSurname);
        vEmail = (TextView)  v.findViewById(R.id.txtEmail);
        vTitle = (TextView) v.findViewById(R.id.title);

        v.setTag(v);
        v.setOnClickListener(this);
        v.setOnLongClickListener(this);
    }


    @Override
    public void onClick(View view) {
        ItemClickListener.onClick(view, getAdapterPosition(),false);
    }

    @Override
    public boolean onLongClick(View view) {
        ItemClickListener.onClick(view, getAdapterPosition(), true);
        return true;
    }
}

要验证应用是否存在以接收意图Intent("com.example.as.a.aa_Detail"),请在您的Intent对象上调用resolveActivity()

ca.SetOnItemClickListener(new ContactAdapter.OnItemClickListener() {
        @Override
        public void onClick(View view, int layoutPosition, boolean b) {

            Intent intent = new Intent("com.example.as.a.aa_Detail");

            if (intent.resolveActivity(getPackageManager()) != null) {
                startActivity(intent);
            }
        }
    });

希望这可以帮助您解决此问题。