gpt4 book ai didi

java - 无法在 FirebaseRecyclerView 中显示子节点数据。数据库异常: Can't convert object to class name

转载 作者:行者123 更新时间:2023-12-02 01:41:19 24 4
gpt4 key购买 nike

我需要显示一个列表(来自 Firebase 实时数据库的数据绑定(bind)到 RecyclerView),该列表位于 root -> History -> UID -> location, date 中。我目前只能显示历史记录下的所有数据。当我尝试将History节点过滤到UID的子节点时出现问题,并给出以下错误:

com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.example.ModelHistory

这是我的 ModelHistory.class:

public class ModelHistory {

String location, date;

// Constructor
public ModelHistory() {

}

public String getLocation() {
return location;
}
// It shows a warning here that setLocation is never used
public void setLocation(String location) {
this.location = location;
}

public String getDate() {
return date;
}

public void setDate(String date) {
this.date = date;
}
}

这是我的RecentHistory.java:

public class RecentHistory extends AppCompatActivity {

RecyclerView mRecyclerView;
FirebaseDatabase mFirebaseDatabase;
DatabaseReference mRef, uidRef;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recent_history);

// Set ActionBar
ActionBar actionBar = getSupportActionBar();
actionBar.setTitle("History");

mRecyclerView = findViewById(R.id.rvHistory);
mRecyclerView.setHasFixedSize(true);


// Set Layout
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));


// Send Query to Firebase Db
mFirebaseDatabase = FirebaseDatabase.getInstance();
String currentuser = FirebaseAuth.getInstance().getCurrentUser().getUid();

mRef = mFirebaseDatabase.getReference("History");
uidRef = mRef.child(currentuser);
Log.d("TAG", uidRef.toString());

}

@Override
protected void onStart() {
super.onStart();
FirebaseRecyclerAdapter<ModelHistory, ViewHolder> firebaseRecyclerAdapter =
new FirebaseRecyclerAdapter<ModelHistory, ViewHolder>(
ModelHistory.class,
R.layout.historyrow,
ViewHolder.class,
mRef
//uidRef // Error when I replace this with mRef
) {
@Override
protected void populateViewHolder(ViewHolder viewHolder, ModelHistory model, int position) {
viewHolder.setHistoryDirectory(getApplicationContext(), model.getLocation(), model.getDate());
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
ViewHolder viewHolder = super.onCreateViewHolder(parent, viewType);
return viewHolder;
}
};
// Set adapter to recycler view
mRecyclerView.setAdapter(firebaseRecyclerAdapter);

}
}

如果需要xml,请告诉我,我将编辑它。

最佳答案

您收到以下错误:

com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.example.ModelHistory

因为当您传递 uidRef 时,这是正确的行为到FirebaseRecyclerAdapter构造函数。

I'm currently only able to display all the data under History.

发生这种情况是因为您的适配器被定义为 FirebaseRecyclerAdapter<ModelHistory, ViewHolder> ,这意味着它应该加载 ModelHistory 类型的所有对象它在您的 mRef 下找到引用一下,其实是正确的。该引用下存在的所有对象的类型均为 ModelHistory .

The problem occurs when I try to filter out the History node to the child node of UID

您不能简单地更改引用并期望以这种方式过滤结果,因为您的适配器仍然是 ModelHistory 类型。并且不是 String 类型。所以当使用uidRef时引用,这意味着您正在尝试加载 ModelHistory 类型的所有对象在该引用之下,显然退出。下root -> History -> UID仅存在 String 类型的属性 ( location and date ),这就是您收到该错误的原因。

如果您想过滤该数据,您应该创建一个如下所示的查询:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference hisotryRef = rootRef.child("History");
Query query = hisotryRef.orderByChild("location").equalTo("Test");

结果在你的 RecyclerView 中只会有一项,在本例中是最后一项,即具有包含 Test 的 location 属性的一项.

除此之外,您正在使用旧版本的 Firebase-UI Library 。我建议您更新到最新版本。

关于java - 无法在 FirebaseRecyclerView 中显示子节点数据。数据库异常: Can't convert object to class name,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54418957/

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