Я пытался показать имена этого списка (выделены синим цветом) в RecyclerView, встроенном во фрагмент:
я хотел показать это вот так я попробовал это в начале:
public void onStart() {
super.onStart();
Query query= FriendRef.child(Current_User);
FirebaseRecyclerOptions<get_setDataUser> options= new FirebaseRecyclerOptions.Builder<get_setDataUser>()
.setQuery(query, get_setDataUser.class)
.build();
FirebaseRecyclerAdapter adapter =
new FirebaseRecyclerAdapter<get_setDataUser, FriendViewHolder>(options) {
@NonNull
@Override
public FriendViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.user_single_layout,parent,false);
FriendViewHolder holder = new FriendViewHolder(view);
return holder;}
@Override
protected void onBindViewHolder(@NonNull final FriendViewHolder friendViewHolder, int position, @NonNull get_setDataUser get_setDataUser) {
String users = getRef(position).getKey();
UserRef.child(users).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
String name = dataSnapshot.child("name").getValue().toString();
String status = dataSnapshot.child("status").getValue().toString();
friendViewHolder.username.setText(name);
friendViewHolder.userStatus.setText(status);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
};
FriendList.setAdapter(adapter);
adapter.startListening();
}
, и это дало мне Ошибка :
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.firebaseauthentication, PID: 15545
com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.example.firebaseauthentication.get_setDataUser
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertBean(com.google.firebase:firebase-database@@16.0.4:423)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToClass(com.google.firebase:firebase-database@@16.0.4:214)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertToCustomClass(com.google.firebase:firebase-database@@16.0.4:79)
at com.google.firebase.database.DataSnapshot.getValue(com.google.firebase:firebase-database@@16.0.4:212)
at com.firebase.ui.database.ClassSnapshotParser.parseSnapshot(ClassSnapshotParser.java:29)
at com.firebase.ui.database.ClassSnapshotParser.parseSnapshot(ClassSnapshotParser.java:15)
at com.firebase.ui.common.BaseCachingSnapshotParser.parseSnapshot(BaseCachingSnapshotParser.java:35)
at com.firebase.ui.common.BaseObservableSnapshotArray.get(BaseObservableSnapshotArray.java:52)
at com.firebase.ui.database.FirebaseRecyclerAdapter.getItem(FirebaseRecyclerAdapter.java:106)
at com.firebase.ui.database.FirebaseRecyclerAdapter.onBindViewHolder(FirebaseRecyclerAdapter.java:122)
at androidx.recyclerview.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:7065)
at androidx.recyclerview.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:7107)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:6012)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6279)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6118)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6114)
at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2303)
at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1627)
at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1587)
at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:665)
at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:4134)
at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3851)
at androidx.recyclerview.widget.RecyclerView.consumePendingUpdateOperations(RecyclerView.java:1897)
at androidx.recyclerview.widget.RecyclerView$1.run(RecyclerView.java:414)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:874)
at android.view.Choreographer.doCallbacks(Choreographer.java:686)
at android.view.Choreographer.doFrame(Choreographer.java:618)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:860)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Итак, я попытался получить список UsersID в Arraylist, затем показать его на RecyclerView, но он не работает правильно, так как я могу заставить его работать:
public void onStart() {
super.onStart();
Query query= FriendRef;
FirebaseRecyclerOptions options= new FirebaseRecyclerOptions.Builder<get_setDataUser>()
.setQuery(query, get_setDataUser.class)
.build();
FirebaseRecyclerAdapter adapter =
new FirebaseRecyclerAdapter<get_setDataUser, FriendViewHolder>(options) {
@NonNull
@Override
public FriendViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.user_single_layout,parent,false);
FriendViewHolder holder = new FriendViewHolder(view);
return holder;}
@Override
protected void onBindViewHolder(@NonNull final FriendViewHolder friendViewHolder, int position, @NonNull get_setDataUser get_setDataUser) {
Users = new ArrayList<String>();
FriendRef.child(Current_User).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot ds:dataSnapshot.getChildren()){
Users.add(ds.getKey()); }
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.w("DataSnapshot","Not reading data");
}
});
for (int i= 0 ; i<Users.size();i++){
String Frind= Users.get(i);
UserRef.child(Frind).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
String name = dataSnapshot.child("name").getValue().toString();
String status = dataSnapshot.child("status").getValue().toString();
friendViewHolder.username.setText(name);
friendViewHolder.userStatus.setText(status);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
}
};
FriendList.setAdapter(adapter);
adapter.startListening();
}
FriendViewHolder :
public static class FriendViewHolder extends RecyclerView.ViewHolder{
TextView username,userStatus;
public FriendViewHolder(@NonNull View itemView) {
super(itemView);
username=itemView.findViewById(R.id.user_single_name);
userStatus=itemView.findViewById(R.id.user_single_status);
}
}
OnCreat :
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
FriendsFragView = inflater.inflate(R.layout.fragment_friends, container, false);
FriendList = FriendsFragView.findViewById(R.id.FriendList);
FriendList.setHasFixedSize(true);
FriendList.setLayoutManager(new LinearLayoutManager(getContext()));
auth = FirebaseAuth.getInstance();
Current_User= auth.getCurrentUser().getUid();
UserRef = FirebaseDatabase.getInstance().getReference().child("Users");
FriendRef = FirebaseDatabase.getInstance().getReference().child("Friends");
UserRef.keepSynced(true);
FriendRef.keepSynced(true);
return FriendsFragView;
}
get_setDataUser Class :
public class get_setDataUser {
String name;
String status;
public get_setDataUser() {
}
public get_setDataUser(String name, String status) {
this.name = name;
this.status = status;
}
public void setName(String name) {
this.name = name;
}
public void setStatus(String status) {
this.status = status;
}
public String getName() {
return name;
}
public String getStatus() {
return status;
}
}