简体   繁体   中英

I cant handle RecyclerView onClick events in A fragment

I have a problem handling simple code on onClick events in my recycler view. Is the error on where i put the onClick event? How do I fix it and why does it happen? Is there any logical mistakes in this? Please tell me everything. BTW I have tried running several kinds of simple code to test the onClick event and none of them have worked. Here is my adapter java class

 package co.quindio.sena.navigationdrawerejemplo.adapters; import android.content.Context; import android.content.Intent; import android.os.Parcelable; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; import java.util.ArrayList; import java.util.HashMap; import co.quindio.sena.navigationdrawerejemplo.Gallery; import co.quindio.sena.navigationdrawerejemplo.MainActivity; import co.quindio.sena.navigationdrawerejemplo.R; import co.quindio.sena.navigationdrawerejemplo.clases.PersonajeVo; import de.hdodenhof.circleimageview.CircleImageView; /** * Created by CHENAO on 13/07/2017. */ public class PersonajesAdapter extends RecyclerView.Adapter<PersonajesAdapter.PersonajeViewHolder>{ ArrayList<PersonajeVo> listaPersonaje; private Context mContext; public PersonajesAdapter(ArrayList<PersonajeVo> listaPersonaje) { this.listaPersonaje=listaPersonaje; ArrayList<String> mImages = new ArrayList<>(); ArrayList<String>mImageNames = new ArrayList<>(); } @Override public PersonajeViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list,null,false); return new PersonajeViewHolder(view); } @Override public void onBindViewHolder(PersonajeViewHolder holder, final int position) { holder.txtNombre.setText(listaPersonaje.get(position).getNombre()); holder.txtInformacion.setText(listaPersonaje.get(position).getInfo()); holder.txtInformacion.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(mContext, "Click",Toast.LENGTH_LONG).show(); } }); } @Override public int getItemCount() { return listaPersonaje.size(); } public class PersonajeViewHolder extends RecyclerView.ViewHolder { TextView txtNombre,txtInformacion; ImageView foto; public PersonajeViewHolder(View itemView) { super(itemView); txtNombre= (TextView) itemView.findViewById(R.id.idNombre); txtInformacion= (TextView) itemView.findViewById(R.id.idInfo); foto= (ImageView) itemView.findViewById(R.id.idImagen); } } } 

I think the problem is your mContext , you declare it but not set any value, so please update your constructor as below

public PersonajesAdapter(Context context, ArrayList<PersonajeVo> listaPersonaje) {
        this.listaPersonaje = listaPersonaje;
        this.mContext = context;
        ArrayList<String> mImages = new ArrayList<>();
        ArrayList<String> mImageNames = new ArrayList<>();
    }

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