-1

Sorry, i have a problem, i have created ad adapter for a recyclerView to click on one of my item but it doesn't work, it is very frustrating, do you have any ideas? I have used it in a old application and it works properly but now i don't know why it doesn't work... Here is my adapter:

public class TecniciAdapter extends RecyclerView.Adapter<TecniciAdapter.MyViewHolder> {

private Context mContext;
private List<Utenti> tecniciList;
private OnTecniciListner onTecniciListner;

public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
    public TextView nome;
    public TextView cognome;
    public TextView stato;
    public OnTecniciListner onTecniciListner;


    public MyViewHolder(View view, OnTecniciListner onTecniciListner) {
        super(view);
        nome = view.findViewById(R.id.textNome);
        cognome = view.findViewById(R.id.textCognome);
        stato = view.findViewById(R.id.textCurrentState);
        this.onTecniciListner = onTecniciListner;
        view.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        onTecniciListner.onTecniciClick(getAdapterPosition());
    }
}

public TecniciAdapter(Context context, ArrayList<Utenti> list, OnTecniciListner onTecniciListner) {
    this.mContext = context;
    this.tecniciList = list;
    this.onTecniciListner = onTecniciListner;
}

@NonNull
@Override
public TecniciAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    View view = LayoutInflater.from(mContext)
            .inflate(R.layout.technician_item, viewGroup, false);

    return new TecniciAdapter.MyViewHolder(view, onTecniciListner);
}

@Override
public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int i) {
    myViewHolder.nome.setText(tecniciList.get(i).getNome());
    myViewHolder.cognome.setText(tecniciList.get(i).getCognome());
    myViewHolder.stato.setText(tecniciList.get(i).getStato());
}

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

public interface OnTecniciListner{
    void onTecniciClick(int position);
}

}

and this is the part in the fragment:

TecniciAdapter adapter = new TecniciAdapter(getActivity(), tecniciList, TecniciFragment.this);
                RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
                recyclerView.setLayoutManager(mLayoutManager);
                recyclerView.setAdapter(adapter);

I have also this in my fragment:

    @Override
public void onTecniciClick(int position) {
    Utenti tecnici = tecniciList.get(position);
    Intent intent = new Intent(getActivity(), LoginActivity.class);
    startActivity(intent);
}
pazz98
  • 73
  • 1
  • 9
  • Does this answer your question? [How to add Onclick listener to recycler view](https://stackoverflow.com/questions/44151979/how-to-add-onclick-listener-to-recycler-view) – Biscuit Mar 20 '20 at 20:33
  • "It doesn't work" isn't descriptive enough to help people understand your problem. Instead, describe what the exact observed behavior is and what the expected behavior should be. For UI issues, a screenshot or video is usually helpful. Include the exact wording of any error messages (including the full [stack trace](https://stackoverflow.com/a/23353174) of any exception, if applicable, as well as which line of code is producing it). Please see [ask] and [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). – Ryan M Mar 20 '20 at 20:54
  • it isn't a error, if i click on item of RecyclerView, it doesn't do anything, i have altro tried to show a toast in my onTecniciClick but it doesn't do anything, it is like that i have never touch the item – pazz98 Mar 20 '20 at 21:34
  • @pazz98 answer by oussama-zaoui should work if you are ok with getting rid of your listener – Manoj Mohanty Mar 20 '20 at 22:42

2 Answers2

0

You do not need to create an instance of OnTecniciListner in your ViewHolder class, the one in the TecniciAdapter should work just fine.

So your MyViewHolder class should look like this

public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
    public TextView nome;
    public TextView cognome;
    public TextView stato;


    public MyViewHolder(View view) {
        super(view);
        nome = view.findViewById(R.id.textNome);
        cognome = view.findViewById(R.id.textCognome);
        stato = view.findViewById(R.id.textCurrentState);
        view.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        onTecniciListner.onTecniciClick(getAdapterPosition());
    }
}

0

in onBindViewHoldertry this :

myViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });