gpt4 book ai didi

android - DataSnapshot 缺少一个没有参数的构造函数

转载 作者:行者123 更新时间:2023-11-29 14:21:21 24 4
gpt4 key购买 nike

我正在我的应用程序中实现 Firebase Recyclerview UI。我已经实现了一个 recyclerview 适配器,它显示了以下异常。以下是我的适配器代码:

FirebaseRecyclerAdapter<DataSnapshot, MyHolder> recyclerAdapter = new FirebaseRecyclerAdapter<DataSnapshot, MyHolder>(
DataSnapshot.class,
R.layout.row_slots,
MyHolder.class,
databaseReference.child("slots").child(uid).child(dayOfWeek).child("time")
) {
@Override
protected void populateViewHolder(MyHolder viewHolder, DataSnapshot model, int position) {

System.out.println("Key : "+model.getKey());

}

};

它显示以下异常: enter image description here

如何使用 FirebaseRecyclerAdapter 获取快照值?

最佳答案

问题是 DataSnapShot 缺少一个无参数 构造函数,因此您不能像这样直接使用它。

Use some other model class instead.

像这样:

创建您自己的名为 FriendlyMessage 的模型:

public class FriendlyMessage {

private String text;
private String name;

// Created constructor with no Arguments
public FriendlyMessage() {
}

public FriendlyMessage(String text, String name) {
this.text = text;
this.name = name;
}

public String getText() {
return text;
}

public void setText(String text) {
this.text = text;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}

Most important thing we have created constructor with no Arguments which was missing in DataSnapShot

注意:上面的模型类只是一个例子,因为您是 firebase 的新手。您可以拥有自己的模型类和您自己的参数类型。

像这样在您的 Firebase Recyclerview 中使用它:

FirebaseRecyclerAdapter mFirebaseAdapter = new FirebaseRecyclerAdapter<FriendlyMessage, MyHolder>(
FriendlyMessage.class,
R.layout.row_slots,
MyHolder.class,
databaseReference.child("slots").child(uid).child(dayOfWeek).child("time")) {

@Override
protected void populateViewHolder(MyHolder viewHolder, FriendlyMessage friendlyMessage, int position)
{

}
};

编辑:

It also matter how you are pushing the data on Database. Do it directly with your model class.

例如上面的模型 FriendlyMessage 像这样推送它:

FriendlyMessage friendlyMessage = new FriendlyMessage("message", "Username");
databaseReference.child(MESSAGES_CHILD).push().setValue(friendlyMessage);

这里您的 child() 将与我的实现有些不同,这只是一个示例。

用于收听特定的 DataSnapShot :

databaseReference.child("users").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
//The PUSH ID OP WANTED

System.out.println("Push Id ---"+postSnapshot.getKey());
}
}

@Override
public void onCancelled(DatabaseError databaseError) {

}
})

Explaining the above listener. It will give you the DataSnapShot Object for your every user who is falling inside your child "users". You can access Push id by getKey() method.

关于android - DataSnapshot 缺少一个没有参数的构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38144597/

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