【问题标题】:How to use Firebase Query for data with anonymous keys如何使用 Firebase 查询具有匿名键的数据
【发布时间】:2018-10-18 17:21:03
【问题描述】:

我有以下数据库数据,我打算使用 FirebaseListAdapter 在 ListView 上显示

我的问题是创建一个查询,因为日期之后的子元素是匿名的。这是查询代码

 Query query = FirebaseDatabase.getInstance().getReference().child("Updates").child(refMail).child(day)
            .orderByKey();

refMail 和 day 分别是用户的电子邮件地址和日期。

这也是我的数据模型类

public class NotesDataModel {

private String Note;
private String uid;
private String time;

public NotesDataModel(){

}

public NotesDataModel(String Note, String uid, String time){
    this.Note=Note;
    this.uid=uid;
    this.time=time;
}

public String getNote() {
    return Note;
}

public void setNote(String note) {
    Note = note;
}

public String getUid() {
    return uid;
}

public void setUid(String uid) {
    this.uid = uid;
}

public String getTime() {
    return time;
}

public void setTime(String time) {
    this.time = time;
}}

最后是适配器初始化

 FirebaseListOptions<NotesDataModel> options = new FirebaseListOptions.Builder<NotesDataModel>()
            .setQuery(query, NotesDataModel.class)
            .setLayout(R.layout.notes_cell_layout)
            .build();

    mAdapter = new FirebaseListAdapter<NotesDataModel>(options) {
        @Override
        protected void populateView(View view, NotesDataModel note, int position) { //some code }}; notesList.setAdapter(mAdapter);

以前的版本是这样的

ref = FirebaseDatabase.getInstance().getReferenceFromUrl(FactoryDaftari.firebaseURL + "Updates/" + refMail + "/" + day);

以及Adapter初始化

mAdapter = new FirebaseListAdapter<NotesDataModel>(this, NotesDataModel.class, R.layout.notes_cell_layout, ref) {

        @Override
        protected void populateView(View view, NotesDataModel note, int position) { }};

【问题讨论】:

    标签: firebase firebase-realtime-database firebaseui


    【解决方案1】:

    您将无法使用数据的结构方式进行此查询。在 NoSQL 数据库中制作数据副本是很常见的,这些数据的结构是为了专门的查询。因此,如果要查询笔记列表,则需要一个结构,其中所有笔记都是同一父级的子级,然后针对该结构进行查询。

    (此外,像现在这样,按带有日期的节点组织笔记,一开始可能甚至不是最好的一般结构。)

    【讨论】:

    • 它曾经与不需要 Query 和 FirebaseListOptions 的以前版本的 Firebase UI 一起使用。在我更新并将代码结构更改为我发布的内容后,问题就开始了
    • 那么你可能想在你的问题中说清楚。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-11
    • 2018-05-17
    • 2021-10-11
    • 1970-01-01
    • 2013-05-18
    • 1970-01-01
    相关资源
    最近更新 更多