我正在尝试在Firebase Recycler Adapter中获取userId,并使用ViewHolder和UserProfile来获取用户名,状态,userID等。但是,当尝试获取用户ID并传递到另一个片段时,出现“无法在child()中为参数'pathString'传递空值”
这是我在SearchFragment和UserDetailFragment中的代码
public void firebaseSearch(String searchText){
Query firebaseSearchQuery = reference.orderByChild("username").startAt(searchText).endAt(searchText + "\uf8ff");
DatabaseReference userDetail = FirebaseDatabase.getInstance().getReference("User");
FirebaseRecyclerAdapter<UserProfile, ViewHolder> recyclerAdapter =
new FirebaseRecyclerAdapter<UserProfile, ViewHolder>(
UserProfile.class,
R.layout.user_search_list,
ViewHolder.class,
firebaseSearchQuery
) {
@Override
protected void populateViewHolder(ViewHolder viewHolder, final UserProfile userProfile, final int i) {
viewHolder.setDetails(userProfile.getUsername(), userProfile.getUsername());
viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bundle bundle = new Bundle();
bundle.putString("username", userProfile.getUsername());
bundle.putString("display_name", userProfile.getDisplay_name());
bundle.putString("status", userProfile.getStatus());
bundle.putString("bio", userProfile.getBio());
bundle.putString("user_id", userProfile.getUserID());
UserDetailFragment userDetailFragment = new UserDetailFragment();
userDetailFragment.setArguments(bundle);
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fl_container, userDetailFragment);
fragmentTransaction.commit();
}
});
}
};
recyclerView.setAdapter(recyclerAdapter);
}
//This is the code in UserDetailFragment
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_user_detail,
container, false);
username = v.findViewById(R.id.user_profile_username);
display_name = v.findViewById(R.id.user_profile_display_username);
status = v.findViewById(R.id.user_profile_status);
bio = v.findViewById(R.id.user_profile_bio);
follow = v.findViewById(R.id.user_profile_follow);
Bundle bundle = getArguments();
String Username = bundle.getString("username");
String DisplayName = bundle.getString("display_name");
String Status = bundle.getString("status");
String Bio = bundle.getString("bio");
user_id = bundle.getString("user_id");
username.setText(Username);
display_name.setText(DisplayName);
status.setText(Status);
bio.setText(Bio);
reference.child(userID).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
UserProfile userProfile = dataSnapshot.getValue(UserProfile.class);
followerUsername = userProfile.getUsername();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
reference.child(user_id).addValueEventListener(new ValueEventListener() { //this is where the error causes
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
UserProfile userProfile = dataSnapshot.getValue(UserProfile.class);
followingUsername = userProfile.getUsername();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
reference.child(userID).child("following").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (!dataSnapshot.hasChild(user_id)) {
follow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
userFollow();
}
});
}else follow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
userUnfollow();
}
});
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
return v;
}