我想获取itemclick
中recyclerview
的密钥
在firebaserecycler
中是getRef(position).getKey();
没有getref方法,如何实现呢?
当前代码
适配器
public class IdAdapter extends RecyclerView.Adapter<IdAdapter.ChatViewHolder> {
private List<IdHelper> mUsers;
private String mId;
private static IdAdapter.ClickListener clickListener;
DatabaseReference mDatabaseReference;
public IdAdapter(List<IdHelper> mUsers) {
this.mUsers = mUsers;
}
public class ChatViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
public TextView mName;
public TextView mStatus;
public CircularImageView mImage;
public ChatViewHolder(View view) {
super(view);
view.setOnClickListener(this);
view.setOnLongClickListener(this);
mName = (TextView) view.findViewById(R.id.display_name);
mStatus = (TextView) view.findViewById(R.id.display_status);
mImage = (CircularImageView) view.findViewById(R.id.circleImageView);
mId = FirebaseAuth.getInstance().getCurrentUser().getUid();
mDatabaseReference = FirebaseDatabase.getInstance().getReference();
}
@Override
public void onClick(View v) {
clickListener.onItemClick(getAdapterPosition(), v);
}
@Override
public boolean onLongClick(View v) {
clickListener.onItemLongClick(getAdapterPosition(), v);
return false;
}
}
public void setOnItemClickListener(IdAdapter.ClickListener clickListener) {
IdAdapter.clickListener = clickListener;
}
public interface ClickListener {
void onItemClick(int position, View v);
void onItemLongClick(int position, View v);
}
@Override
public IdAdapter.ChatViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View V = LayoutInflater.from(parent.getContext()).inflate(R.layout.custom_activity_id, parent, false);
return new IdAdapter.ChatViewHolder(V);
}
//BindViewHolder..................................................................................//
@Override
public void onBindViewHolder(IdAdapter.ChatViewHolder holder, int position) {
final IdHelper mHelper = mUsers.get(position);
holder.mName.setText(mHelper.getName());
holder.mStatus.setText(mHelper.getStatus());
Picasso.get().load(mHelper.getImage()).into(holder.mImage);
}
//ItemCount.......................................................................................//
@Override
public int getItemCount() {
return mUsers.size();
}
}
分段
mAdapter.setOnItemClickListener(new IdAdapter.ClickListener() {
@Override
public void onItemClick(int position, View v) {
Intent intent = new Intent(getActivity(), Chat.class);
intent.putExtra("Recievers_Id", userId);
intent.putExtra("Recievers_Name", mName);
startActivity(intent);
}
@Override
public void onItemLongClick(int position, View v) {
Log.d("onItemLongClick", userId);
}
});
我单击显示相同用户名的任何项目。我意识到了这个问题,因为我根本没有使用位置,因此它只返回一个id。如何根据点击获得ID?
最佳答案
您所做的正确,只是未使用您通过视图侦听器从视图持有者传递到片段的位置。
mAdapter.setOnItemClickListener(new IdAdapter.ClickListener() {
@Override
public void onItemClick(int position, View v) {
Intent intent = new Intent(getActivity(), Chat.class);
userId = mUsers.get(position).getUserId();
intent.putExtra("Recievers_Id", userId);
intent.putExtra("Recievers_Name", mName);
startActivity(intent);
}
@Override
public void onItemLongClick(int position, View v) {
Log.d("onItemLongClick", userId);
}
});
只需从usersList获取特定位置的用户。