我正在检索添加但不可见的少数用户列表。当我在项目上添加Toast时,它会显示数据 null ,并且android studio也会在模型类中发出警告,构造函数从不使用。我正在使用ChildEventListener
和自定义适配器
自定义适配器代码
class CustomMemberAdapter extends RecyclerView.Adapter<SetMeeting_2.User_MemberHolder>{
List<UserModel_member_DP_NAME> mList;
CustomMemberAdapter(List<UserModel_member_DP_NAME> mList) {
this.mList = mList;
}
@Override
public User_MemberHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.set_meeting_userlist_item,parent,false);
return new User_MemberHolder(v);
}
@Override
public void onBindViewHolder(User_MemberHolder holder, int position) {
final UserModel_member_DP_NAME model = mList.get(position);
holder.txt.setText(model.getName());
Picasso.with(holder.itemView.getContext()).load(model.getDp()).into(holder.img);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(view.getContext(), String.valueOf(model.getName()), Toast.LENGTH_SHORT).show();
}
});
}
@Override
public int getItemCount() {
return mList.size();
}
}
模型类代码
private static class UserModel_member_DP_NAME {
String dp,name;
UserModel_member_DP_NAME(){}
UserModel_member_DP_NAME(String dp, String name) { //never used warning
this.dp = dp;
this.name = name;
}
public String getDp() {
return dp;
}
public void setDp(String dp) {
this.dp = dp;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
ChildEventListener代码
mDatabase = FirebaseDatabase.getInstance().getReference();
defaultQuery = mDatabase.child("users").child(current_user).child("directorylist");
defaultQuery.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
UserModel_member_DP_NAME model = dataSnapshot.getValue(UserModel_member_DP_NAME.class);
memberList.add(model);
adapter.notifyDataSetChanged();
}
@Override
public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
recyclerView.setAdapter(adapter);