在适配器中,我想获取所选用户的用户ID。但是它显示了一个错误
getRef(位置)
当得到一个key.how来解决这个问题?这是我的OnBindViewHolder,
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
final MyHolder myHolder = (MyHolder) holder;
final Users current = data.get(position);
mUsersDatabase = FirebaseDatabase.getInstance().getReference("Users");
myHolder.name.setText(current.getName());
myHolder.status.setText(current.getStatus());
Picasso.get().load(current.getImage()).placeholder(R.drawable.download).into(myHolder.img);
final String user_id = getRef(position).getKey();
View.OnClickListener profileView = new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent profile_intent = new Intent(mContext,ProfileActivity.class);
profile_intent.putExtra("user_id",user_id);
mContext.startActivity(profile_intent);
}
};
myHolder.itemView.setOnClickListener(profileView);
}
我想获取所选用户的详细信息。
这是我的个人资料活动;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
String user_id = getIntent().getStringExtra("user_id");
mUsersDatabase = FirebaseDatabase.getInstance().getReference().child("Users").child(user_id);
mProfileImage = (ImageView) findViewById(R.id.profile_image);
mProfileName = (TextView) findViewById(R.id.profile_display_name);
mProfileStatus = (TextView) findViewById(R.id.profile_status);
mProfileFirendsCount = (TextView) findViewById(R.id.profile_total_friends);
mSendRequestBtn = (Button) findViewById(R.id.request_btn);
mUsersDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
String display_name = dataSnapshot.child("name").getValue().toString();
String status = dataSnapshot.child("status").getValue().toString();
String image = dataSnapshot.child("image").getValue().toString();
mProfileName.setText(display_name);
mProfileStatus.setText(status);
}
由于在选择任何用户时都需要user_id,因此必须在onClickListener()中使用getAdapterPopse()获取适配器位置,如下所示:
View.OnClickListener profileView = new View.OnClickListener() {
@Override
public void onClick(View v) {
String user_id = getRef(getAdapterPosition()).getKey();
Intent profile_intent = new Intent(mContext,ProfileActivity.class);
profile_intent.putExtra("user_id",user_id);
mContext.startActivity(profile_intent);
}
};