gpt4 book ai didi

java - 从 Firebase 检索数据到 fragment 上嵌入的 RecyclerView

转载 作者:行者123 更新时间:2023-12-04 04:12:16 26 4
gpt4 key购买 nike

我试图在嵌入 fragment 的 RecyclerView 上显示此列表的名称(以蓝色突出显示): [1]: /image/RwnAP.png

我想像这样给他们看我一开始就试过这个:

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)

所以我尝试在 Arraylist 中获取 UserIDs 列表,然后将其显示在 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 类:

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;
}
}

最佳答案

无法将 String 类型的对象转换为 get_setDataUser 类型。你 friend 的 child 是一个字符串。你用蓝线表示。您必须像这样更改您的选项和适配器:

 FirebaseRecyclerOptions<String> options= new FirebaseRecyclerOptions.Builder<String>()
.setQuery(query, String.class)
.build();
FirebaseRecyclerAdapter adapter =
new FirebaseRecyclerAdapter<String, 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 String userid) {
UserRef.child(userid).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
// if you want you can convert dataSnapshot to get_setDataUser here
// get_setDataUser user = dataSnapshot.getValue(get_setDataUser.class);
// friendViewHolder.username.setText(user.getName());
// friendViewHolder.userStatus.setText(user.getStatus());
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);

关于java - 从 Firebase 检索数据到 fragment 上嵌入的 RecyclerView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61511844/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com