将数据从 Firebase 检索到嵌入在 Fragment 上的 RecyclerView
Posted
技术标签:
【中文标题】将数据从 Firebase 检索到嵌入在 Fragment 上的 RecyclerView【英文标题】:Retrieve Data from Firebase to a RecyclerView Embedded on Fragment 【发布时间】:2020-08-14 03:13:32 【问题描述】:我试图在嵌入片段的 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)
所以我尝试在 Arraylist 中获取用户 ID 列表,然后将其显示在 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;
【问题讨论】:
将您的数据库添加到您的问题中,而不是作为链接。 【参考方案1】:无法将 String 类型的对象转换为 get_setDataUser 类型。你朋友的孩子是一个字符串。你用蓝线表示。您必须像这样更改您的选项和适配器:
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);
【讨论】:
谢谢。我像这样改变了它,它对我有用以上是关于将数据从 Firebase 检索到嵌入在 Fragment 上的 RecyclerView的主要内容,如果未能解决你的问题,请参考以下文章