我正在从Firebase数据库中检索所有用户,并使用适配器在recyclerview中显示它们。我想获取我单击的用户的userID。我尝试使用下面的代码,但它仅获取当前用户的ID。如何获得在recyclerview中单击的用户的userID。 recyclerview显示用户的图片和名称。任何提示我都会很感激。
这是处理项目点击的代码。这只是获取当前用户的ID
public class FindFriendsActivity extends AppCompatActivity implements UsersAdapter.OnItemClickListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate ( savedInstanceState );
setContentView ( R.layout.activity_find_friends){
recyclerView.setLayoutManager (new LinearLayoutManager ( this ) );
mcontacts = new ArrayList <> ();
mAdapter = new UsersAdapter ( this, mcontacts );
recyclerView.setAdapter ( mAdapter );
mdatabaseRef = FirebaseDatabase.getInstance ().getReference ( "Users" );
mdatabaseRef.addValueEventListener ( new ValueEventListener () {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
mcontacts.clear ();
for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren ()) {
Contacts contacts = dataSnapshot1.getValue(Contacts.class);
mcontacts.add(contacts);
}
mAdapter = new UsersAdapter ( getApplicationContext () , mcontacts );
recyclerView.setAdapter ( mAdapter );
mAdapter.setOnItemClickListener ( FindFriendsActivity.this );
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
} );
}
}
//Handling clicks of users in the recyclerview
@Override
public void onItemClick(int position) {
Contacts conty = mcontacts.get ( position );
FirebaseUser clickedUser = FirebaseAuth.getInstance ().getCurrentUser ();
Toast.makeText ( this , "" + clickedUser.getUid () , Toast.LENGTH_SHORT ).show ();
}
}
适配器类
public class UsersAdapter extends RecyclerView.Adapter<UsersAdapter.UsersViewHolder>{
private Context mcontext;
private List<Contacts> mcontacts;
private OnItemClickListener mListener;
public UsersAdapter(Context context, List<Contacts> contacts){
mcontext = context;
mcontacts = contacts;
}
@NonNull
@Override
public UsersViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup , int i) {
View v = LayoutInflater.from ( mcontext ).inflate ( R.layout.users_display_layout, viewGroup, false );
return new UsersViewHolder ( v );
}
@Override
public void onBindViewHolder(@NonNull UsersViewHolder usersViewHolder , int i) {
Contacts contactsCurrent = mcontacts.get ( i );
usersViewHolder.nameOfUser.setText ( contactsCurrent.getName () ); //'''''''''''''''''''''''''''''
usersViewHolder.nameOfTown.setText ( contactsCurrent.getTownname () ); //''''''''''''''''''''''''''''''
Picasso.get ().load ( contactsCurrent.getImage () ).placeholder ( R.drawable.person_account ).into ( usersViewHolder.usersImage ); //''''''''''''''''''''''''''''''''
}
@Override
public int getItemCount() {
return mcontacts.size ();
}
public class UsersViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
public TextView nameOfUser;
public TextView nameOfTown;
public CircleImageView usersImage;
public UsersViewHolder(@NonNull View itemView) {
super ( itemView );
nameOfUser = itemView.findViewById ( R.id.user_profile_name );
nameOfTown = itemView.findViewById ( R.id.user_town_name );
usersImage = itemView.findViewById ( R.id.users_profile_image );
itemView.setOnClickListener ( this );
usersImage.setOnClickListener ( new View.OnClickListener () {
@Override
public void onClick(View v) {
// Toast.makeText ( v.getContext () , "USER FRIEND" , Toast.LENGTH_SHORT ).show ();
if (mListener != null){
int position = getAdapterPosition ();
if (position != RecyclerView.NO_POSITION){
mListener.onItemClick ( position );
}
}
}
} );
}
@Override
public void onClick(View v) {
if (mListener != null){
int position = getAdapterPosition ();
if (position != RecyclerView.NO_POSITION){
mListener.onItemClick ( position );
}
}
}
}
public interface OnItemClickListener{
void onItemClick(int position);
}
public void setOnItemClickListener(OnItemClickListener listener){
mListener = listener;
}
}