0

im trying to make that when im clicking and item on the RecyclerView , it will open new activity with the details of the clicked item.

my adapter.java code is :

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

private Context context;
private LayoutInflater inflater;
List<UsersData> data= Collections.emptyList();
UsersData current;
int currentPos=0;

// create constructor to innitilize context and data sent from MainActivity
public AdapterUsersData(Context context, List<UsersData> data){
    this.context=context;
    inflater= LayoutInflater.from(context);
    this.data=data;
}

// Inflate the layout when viewholder created
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view=inflater.inflate(R.layout.container_users, parent,false);
    MyHolder holder=new MyHolder(view);
    return holder;
}

// Bind data
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

    // Get current position of item in recyclerview to bind data and assign values from list
    final MyHolder myHolder= (MyHolder) holder;
    final UsersData current=data.get(position);
    myHolder.textFirstName.setText("First Name: " + current.fname);
    myHolder.textPassword.setText("Pass: " + current.password);
    myHolder.textLastName.setText("Last Name: " + current.lname);
    myHolder.textEmail.setText("Email. " + current.email);


}

// return total item from List
@Override
public int getItemCount() {
    return data.size();
}


class MyHolder extends RecyclerView.ViewHolder{

    TextView textFirstName;
    TextView textPassword;
    TextView textLastName;
    TextView textEmail;

    // create constructor to get widget reference
    public MyHolder(View itemView) {
        super(itemView);
        textFirstName= (TextView) itemView.findViewById(R.id.textFirstName);
        textPassword = (TextView) itemView.findViewById(R.id.textPassword);
        textLastName = (TextView) itemView.findViewById(R.id.textLastName);
        textEmail = (TextView) itemView.findViewById(R.id.textEmail);
    }

}

}

For example i want to parse the "curremt.fname ......." to another activity to show the clicked item full details to the user.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
O.Men
  • 1
  • 3

3 Answers3

0

RecyclerViews provide an onclick functionality out o the box. You need to provide onClick functionality yourself. Same goes for long pressing.

You would ideally make your ViewHolder implement the View.OnClickListener interface and inside of the ViewHolder's constructor you'd register it as itemView's click listener. Whenever the onClick method gets called you can get the position of that ViewHolder instance by calling getAdapterPostion(). Which will return an int that indicates the position of the View relative to the Adapters data set. From here you can call a regisster custom callback. What I've done in my app, is create a custom onItemClick interface with a method that gets fired and receives the position of the int and the view that was clicked. I've modified you code to give you an example of what you can do.

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

private Context context;
private LayoutInflater inflater;
List<UsersData> data= Collections.emptyList();
UsersData current;
int currentPos=0;

// create constructor to innitilize context and data sent from MainActivity
public AdapterUsersData(Context context, List<UsersData> data){
    this.context=context;
    inflater= LayoutInflater.from(context);
    this.data=data;
}

// Inflate the layout when viewholder created
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view=inflater.inflate(R.layout.container_users, parent,false);
    MyHolder holder=new MyHolder(view);
    return holder;
}

// Bind data
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

    // Get current position of item in recyclerview to bind data and assign values from list
    final MyHolder myHolder= (MyHolder) holder;
    final UsersData current=data.get(position);
    myHolder.textFirstName.setText("First Name: " + current.fname);
    myHolder.textPassword.setText("Pass: " + current.password);
    myHolder.textLastName.setText("Last Name: " + current.lname);
    myHolder.textEmail.setText("Email. " + current.email);


}

// return total item from List
@Override
public int getItemCount() {
    return data.size();
}

public interface OnItemClickListener {
    void onItemClicked(View v, int position);
}

private OnItemClickListener listener;


class MyHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    TextView textFirstName;
    TextView textPassword;
    TextView textLastName;
    TextView textEmail;

    // create constructor to get widget reference
    public MyHolder(View itemView) {
        super(itemView);
        itemView.setOnClickListener(this);
        textFirstName= (TextView) itemView.findViewById(R.id.textFirstName);
        textPassword = (TextView) itemView.findViewById(R.id.textPassword);
        textLastName = (TextView) itemView.findViewById(R.id.textLastName);
        textEmail = (TextView) itemView.findViewById(R.id.textEmail);
    }

    public void onClick(View v) {
        if(listener != null) {
            listener.onItemClicked(v, getAdapterPosition());
        }
    }

}
cincy_anddeveloper
  • 1,140
  • 1
  • 9
  • 19
  • Didnt worked for me .... i tried to do : public void onClick(View v) { if(listener != null) { listener.onItemClicked(v, getAdapterPosition()); Toast.makeText(context,"fdfdfd",Toast.LENGTH_LONG); } } and it dosent make the toast – O.Men Nov 21 '17 at 17:20
  • i tried to add a Toast msg when it clicked , and the msg didnt showed – O.Men Nov 21 '17 at 17:24
  • I just copied your code and added this : public void onClick(View v) { if(listener != null) { listener.onItemClicked(v, getAdapterPosition()); Toast.makeText(context,"fdfdfd",Toast.LENGTH_LONG); } } – O.Men Nov 21 '17 at 17:27
  • @O.Men You shouldn't have blindly copied the code, you need to first understand what I am doing. There is no registered listener so the null check will always fail thus never executing what's inside of the if block. You need to write the code to register the OnItemClickListener. – cincy_anddeveloper Nov 21 '17 at 17:29
  • Im sorry , i new to this, i understand what u did , but i dont know hot to register the OnItemClickListener... – O.Men Nov 21 '17 at 17:37
  • @O.Men you need to write a registration method yourself. Just write a method called setOnItemClickListener(onItemClickListener listener) and assign the listener reference. – cincy_anddeveloper Nov 21 '17 at 17:52
0

You can try..

class dapterUsersData extends RecyclerView.Adapter<dapterUsersData.ViewHolder> {

    private Context context;
    private LayoutInflater inflater;
    List<UsersData> data =  Collections.emptyList();
    ArrayList<UsersData> dataSet;

    public dapterUsersData(ArrayList<UsersData> data) {
        this.data = data;
    }


    @Override
    public dapterUsersData.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {

        View itemLayoutView = LayoutInflater.from(viewGroup.getContext()).inflate(
                R.layout.container_users, null);
        itemLayoutView.setLayoutParams(new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, RecyclerView.LayoutParams.WRAP_CONTENT));


        dapterUsersData.ViewHolder viewHolder = new dapterUsersData.ViewHolder(itemLayoutView);

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(dapterUsersData.ViewHolder viewHolder, int i) {

        UsersData current = data.get(i);

        viewHolder.textFirstName.setText("First Name: " + current.getFname());
        viewHolder.textPassword.setText("Pass: " + current.getPassword());
        viewHolder.textLastName.setText("Last Name: " + current.getLname());
        viewHolder.textEmail.setText("Email. " + current.getEmail());

        viewHolder.menu = current;
    }

    @Override
    public int getItemCount() {

        return dataSet.size();
    }

    public void updateList(List<UsersData> temp) {
        dataSet = (ArrayList<UsersData>) temp;
        notifyDataSetChanged();
    }

    // inner class to hold a reference to each item of RecyclerView
    public  class ViewHolder extends RecyclerView.ViewHolder {

        TextView textFirstName;
        TextView textPassword;
        TextView textLastName;
        TextView textEmail;




        public UsersData menu;

        public ViewHolder(View itemLayoutView) {
            super(itemLayoutView);

            textFirstName= (TextView) itemLayoutView.findViewById(R.id.textFirstName);
            textPassword = (TextView) itemLayoutView.findViewById(R.id.textPassword);
            textLastName = (TextView) itemLayoutView.findViewById(R.id.textLastName);
            textEmail = (TextView) itemLayoutView.findViewById(R.id.textEmail);


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

                    //  OnClick
                    String firstName = textFirstName.getText().toString();

                }
            });

        }

    }
}
//end Adapter
Enamul Haque
  • 4,789
  • 1
  • 37
  • 50
  • If you have still any problem address me..because it is working for me.. – Enamul Haque Nov 21 '17 at 17:30
  • did but i tried to add a Toast msg and it didnt worked,can you adress me ? – O.Men Nov 21 '17 at 17:40
  • In itemLayoutView.setOnClickListener you can write String firstName = textFirstName.getText().toString(); Toast.makeText(YourActivity.this,firstName,Toast.LENGTH_LONG).show(); – Enamul Haque Nov 21 '17 at 17:44
0

Thank you guys for the help!

anyway i just added :

 myHolder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(context,current.fname.toString(),Toast.LENGTH_LONG).show();
        }
    });

To the : public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) method , and it worked.

O.Men
  • 1
  • 3