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.
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.