gpt4 book ai didi

android - 使用带有 addValueEventListener 的查询

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

我正尝试在我的 Android 应用程序中从 Firebase 获取特定数据。在 onCreate 中,我定义了我的 DatabaseReference 和 ArrayList:

databaseRef = FirebaseDatabase.getInstance().getReference("books");
books = new ArrayList<>();

然后用数据填充我的 ListView:

 @Override
protected void onStart() {
super.onStart();
databaseRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
books.clear();
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
String uid = user.getUid();
//iterating through all the nodes
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
Book bookrequest = postSnapshot.getValue(Book.class);
books.add(bookrequest );
}
}
BooksListing booksAdapter = new BooksListing(MainActivity.this, books);
//attaching adapter to the listview
listViewBooks.setAdapter(booksAdapter);
}

@Override
public void onCancelled(DatabaseError databaseError) {

}
});
}

现在我只想从 Firebase 获取具有我自己的 UID 的书籍。

type books {
id: String;
amount: Number;
uid: uid;}

在将数据传输到我的 Android 设备之前,我可以在哪里执行查询?

最佳答案

试试这个:

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
String uid = user.getUid();

databaseRef.orderByChild("uid").equalTo(uid)
.addValueEventListener(new ... {
... onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
// here will only contain books that have
// uid equal to your user uid
}
...
}
...
});

希望对你有帮助

关于android - 使用带有 addValueEventListener 的查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42987031/

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