I'm unable to order result with orderByChild on a date field (timestamp)
Here the structure of data :
And the code :
DatabaseReference mMessagesRef = FirebaseDatabase.getInstance().getReference().child("messages")
.child(mConversationId).getRef()
.orderByChild("date").getRef();
mMessagesRef.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot data: dataSnapshot.getChildren()){
The query returned the value, but not ordered by date, it's return by key..
Try this:
DatabaseReference mMessagesRef=FirebaseDatabase.getInstance().getReference();
Query queryRef = mMessagesRef.child("messages").orderByChild("date");
queryRef.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot data: dataSnapshot.getChildren()){
Related
This question already has answers here:
How to arrange firebase database data in ascending or descending order?
(2 answers)
Closed 1 year ago.
I am developing an android chat application in which I need to order the conversation details by the timestamp. My firebase data structure is mentioned below .
storing data like this
Now I want to retrieve and show the data in decending/latest order on my RecyclerView from firebase realtime database based on timestamp.
I have tried the following approaches.
final DatabaseReference nm= FirebaseDatabase.getInstance().getReference("Transaction");
Query query = nm.orderByChild("Timestamp")
.limitToLast(5);
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
listData.clear();
if (dataSnapshot.exists()){
for (DataSnapshot npsnapshot : dataSnapshot.getChildren()){
Transaction ld= npsnapshot.getValue(Transaction.class);
listData.add(ld);
}
Tadapter=new TransactionAdapter(listData);
rv.setAdapter(Tadapter);
Log.d(TAG,"Total Count"+Tadapter.getItemCount());
}
}
I think you are doing minor mistake in this code
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
listData.clear();
if (dataSnapshot.exists()){
for (DataSnapshot npsnapshot : dataSnapshot.getChildren()){
Transaction ld= npsnapshot.getValue(Transaction.class);
listData.add(ld);
}
Tadapter=new TransactionAdapter(listData);
rv.setAdapter(Tadapter);
Log.d(TAG,"Total Count"+Tadapter.getItemCount());
}
}
You are doing a query to singleEvent you have to do a query for addValueEventListener
query.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
listData.clear();
if (dataSnapshot.exists()){
for (DataSnapshot npsnapshot : dataSnapshot.getChildren()){
Transaction ld= npsnapshot.getValue(Transaction.class);
listData.add(ld);
}
Tadapter=new TransactionAdapter(listData);
rv.setAdapter(Tadapter);
Log.d(TAG,"Total Count"+Tadapter.getItemCount());
}
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
}
});
How to fetch this Data
I only want number and their respective bidding amount.
This id my code:
databaseReference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
Map<String, Object> map = (HashMap<String, Object>) dataSnapshot.child(statusScreenLoaders.get(position).getItemName()).getValue();
for ( String key : map.keySet() ) {
Log.d(TAG, "onDataChange: "+key);
}
for (Object value:map.values()){
Log.d(TAG, "onDataChange: " +value);
}
You can do the following to get the number:
DatabaseReference ref = FirebaseDatabase.getInstance().getReference("KitKat");
ref.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()){
String key = ds.getKey();
}
}
String key = ds.getKey(); will return then number that you want. You can then create another reference inside the for loop to get the data of the binding amount:
DatabaseReference ref = FirebaseDatabase.getInstance().getReference("KitKat");
ref.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()){
String key = ds.getKey();
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("KitKat").child(key);
reference.child("BiddingAmount").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
String num = dataSnapshot.child("0").getValue(String.class);
}
}
}
I want to search through data in firebase like here:
I have already tried, but it not works
//Query query = FirebaseDatabase.getInstance().getReference(uid).orderByChild("title").startAt(editable.toString()).endAt(editable.toString()+"\uf888");
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("categoreis");
Query query = FirebaseDatabase.getInstance().getReference("categoreis").limitToFirst(4).orderByChild("image");
query.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
list.clear();
for (DataSnapshot data:dataSnapshot.getChildren())
{
for (DataSnapshot ds:data.child("SubAccessories").getChildren())
{
for (DataSnapshot ds1:ds.getChildren())
{
//Log.d("checkData1",ds1.toString());
//Log.d("checkData1",ds1.child("image").getValue(String.class));
}
}
}
If you want to get the image as I see in your question, please use the following lines of code but assuming that the categories node is a direct child of your Firebase database root:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference ref = rootRef.child("categories/Accessories/SubAccessories/Accessories/Grab Bar");
ValueEventListener valueEventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String image = dataSnapshot.child("image").getValue(String.class);
Log.d("TAG", image);
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
ref.addListenerForSingleValueEvent(valueEventListener);
According to your comment, you should loop through the second Accessories node using getChildren() method like this:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference ref = rootRef.child("categories/Accessories/SubAccessories/Accessories");
ValueEventListener valueEventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String image = dataSnapshot.child("image").getValue(String.class);
Log.d("TAG", image);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
ref.addListenerForSingleValueEvent(valueEventListener);
I am trying to use Firebase in my android application. I need to get an object, i am searching with its email (attribute in the object).
this is my code of search:
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("Remorqueur");
Query queryRef=myRef.orderByChild("email").equalTo(ed_mail.getText().toString());
If the object exists in my database I need to get the whole object back! how can I do it?
should be able to do something like following
queryRef.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot childSnapshot: dataSnapshot.getChildren()) {
MyPojo app = childSnapshot.getValue(MyPojo.class);
}
Try this Please
DatabaseReference dbRef = firebaseDatabase.getReference("Remorqueur");
Query query = profileDbRef
.orderByChild("email")
.equalTo(ed_mail.getText().toString());
query.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
Log.d(TAG, dataSnapshot1.getKey() + " " + dataSnapshot1.getValue(Profile.class)
.toString());
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
My Database looks like this
To get items I do this:
mFirebaseDatabase
.getReference("credits")
.orderByChild("regionId")
.equalTo(2607)
.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
List<Credit> creditList = new ArrayList<Credit>();
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
Credit credit = postSnapshot.getValue(Credit.class);
creditList.add(credit);
}
}
});
How to make query "where regionId==2607 OR regionId==0"?
How to make query "where 'requiredDocuments'.contains("Passport")"?