我正试图将持有人user.getID
传递到我的出勤活动中,如何将用户ID从我的适配器传递到出勤活动中
这是我的出勤活动
reference = FirebaseDatabase.getInstance().getReference("parent");
intent = getIntent();
final String currentSubject = intent.getStringExtra("currentSubject");
reference.addValueEventListener(new ValueEventListener() {
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
final User user = snapshot.getValue(User.class);
ZoneId zonedId = ZoneId.of("America/Montreal");
final LocalDate today = LocalDate.now(zonedId);
reference = FirebaseDatabase.getInstance().getReference("attendance").child(user.getSchoolcode()).child(grade.getText().toString() + " " + section.getText().toString()).child(currentSubject).child(today.toString()).child(userID);
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
HashMap<String, Object> hashMap = new HashMap<>();
hashMap.put("attendance", "yes");
reference.updateChildren(hashMap);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
我想将其从适配器传递到此 这是我的适配器
public class StudentAdapter extends RecyclerView.Adapter<StudentAdapter.ViewHolder> {
private Context context;
private List<User> users;
public StudentAdapter(Context context, List<User> users) {
this.context = context;
this.users = users;
}
DatabaseReference reference;
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.student_item, parent, false);
return new StudentAdapter.ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull final ViewHolder holder, int position) {
final User user = users.get(position);
holder.studentName.setText(user.getStudentname() + " " + user.getLastname());
}
@Override
public int getItemCount() {
return users.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView studentName;
public Switch aSwitch;
public ViewHolder(View itemView) {
super(itemView);
studentName = itemView.findViewById(R.id.studentName);
aSwitch = itemView.findViewById(R.id.attendanceSwitch);
}
}
这是我的适配器,用于保存recyclerview,我正在尝试获取参加活动recyclerview时查看过的用户ID
答案 0 :(得分:0)
一种使用方式是callback
:
在您的adapter
中定义这样的接口:
public class StudentAdapter extends RecyclerView.Adapter<StudentAdapter.ViewHolder> {
private SendCallBack mCallback;
interface SendCallBack {
void send(int id);
}
//Initial callback in constructor
public StudentAdapter(Context context, List<User> users,SendCallBack callback) {
this.context = context;
this.users = users;
mCallback = callback;
}
//Now in where that you want to pass data use this
mCallback.send(user.getID);
}
现在在您的activity
中实现callback
就像这样:
public class YourActivty implements YourAdapter.SendCallback {
@Override
public void send(int id) {
}
//For pass callBack for your adapter constructor use this
}
对于其他方式,您可以使用this链接。