[英]How to retrieve last child from Firebase (java)?
DatabaseReference lastRef = FirebaseDatabase.getInstance().getReference().child("Ranklist");
Query lastQuery = lastRef.orderByKey().limitToLast(1);
lastQuery.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot data) {
newUser.Rank=Long.parseLong(data.getKey().toString())+1;
OrderConsoleMessages();
texts[0].setText("Last Rank is: "+newUser.Rank+" | UID: " + data.getValue());
}
@Override
public void onCancelled(DatabaseError databaseError) {
OrderConsoleMessages();
texts[0].setText(databaseError.toString());
}
});
My question is how do I retrieve the last child from "Ranklist"?我的问题是如何从“Ranklist”中检索最后一个孩子? I tried to
orderByKey
ByValue but nothing works in my case.我试图
orderByKey
ByValue 但在我的情况下没有任何作用。 I don't get any errors, I think I simply don't get any data from this query.我没有收到任何错误,我想我根本没有从这个查询中得到任何数据。
Edit: This is what I get from data.getValue().toString ->>> {4=somekey4}编辑:这是我从 data.getValue().toString ->>> {4=somekey4} 得到的
your code is working correctly.您的代码工作正常。 I tested it.
我测试了它。 Your query to database returning the last child of the Parent Node "Ranklist".
您对数据库的查询返回父节点“Ranklist”的最后一个子节点。 make your
DatabaseReference dbref
as global member将您的
DatabaseReference dbref
设为全局成员
dbRef = FirebaseDatabase.getInstance().getReference().child("Ranklist");
Query lastQuery = dbRef.orderByKey().limitToLast(1);
lastQuery.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot data : dataSnapshot.getChildren())
{
//if you call methods on dataSnapshot it gives you the required values
String s = data.getValue(); // then it has the value "somekey4"
String key = data.getKey(); // then it has the value "4:"
//as per your given snapshot of firebase database data
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}});
Hope it is helping...希望它有帮助...
Happy Coding :-)快乐编码:-)
Your code is correct but you are getting only Key of the Last Node.您的代码是正确的,但您只获得了最后一个节点的密钥。 So you need to change it with like:
所以你需要像这样改变它:
dbRef.child("Ranklist")
.orderByChild(Constants.ARG_CONV_TIMESTEMP)
.limitToLast(1)
.addChildEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String value = dataSnapshot.getValue();
String key = dataSnapshot.getKey();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}});
You cannot use a query in this case.在这种情况下不能使用查询。 As I see in your screenshot, the
Ranklist
node is an ArrayList
and to get the value of your last child under Ranklist
node, which is somekey4
, please use the following lines of code:正如我在你的屏幕截图看到,
Ranklist
节点是一个ArrayList
,并获得在你的最后一个子项的值Ranklist
节点,这是somekey4
,请用下面的代码行:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference ref = rootRef.child("Ranklist");
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
ArrayList<String> arrayList = (ArrayList<String>) dataSnapshot.getValue();
String lastItem = arrayList.get(arrayList.size() - 1);
Log.d("TAG", lastItem);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.d(TAG, databaseError.getMessage());
}
};
ref.addListenerForSingleValueEvent(valueEventListener);
The output in your logcat will be: somekey4
. logcat 中的输出将是:
somekey4
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.