Вы находитесь на странице: 1из 6

I'm trying to select all the IDs with the same date and populate a recycler view with

it, here
is my code snippet
private void updateValues()
{
if(rAuth !=null && rUser!=null)
{
final String date="15/03/2018";
final DatabaseReference rRef =
FirebaseDatabase.getInstance().getReference()
.child("Users")
.child("Transactions");

rRef.child("Expense").addValueEventListener(new ValueEventListener()
{
@Override
public void onDataChange(DataSnapshot rSnap)
{
for(DataSnapshot d: rSnap.getChildren())
{
rRef.child(d.getKey())
.child("Date")
.addListenerForSingleValueEvent(new ValueEventListener()
{
@Override
public void onDataChange(DataSnapshot dataSnapshot)
{
if(date.equals(dataSnapshot.getValue().toString()))
{
recordItems.add(new recordItems(iconGetter(d.child("Type").getValue().toString()),
d.child("Description").getValue().toString(),
d.child("Type").getValue().toString(),
d.child("Value").getValue().toString(),
d.child("Date").getValue().toString()));
}
}

@Override
public void onCancelled(DatabaseError databaseError)
{

}
});
}
initRecycler();
}

@Override
public void onCancelled(DatabaseError databaseError)
{

}
});
2 Answers
First of all you should fix those code line and add the User-id:

final DatabaseReference rRef =


FirebaseDatabase.getInstance().getReference()
.child("Users")
.child("Transactions");
Should look like that:

final DatabaseReference rRef =


FirebaseDatabase.getInstance().getReference()
.child("Users")
.child(userId)
.child("Transactions");
Now, if you want to make your code more efficient you can get only the desired dates with
the next query:

final Query refQuery =


FirebaseDatabase.getInstance().getReference()
.child("Users")
.child(userId)
.child("Transactions")
.child("Expense")
.orderByChild("Date").equalTo(date);
Then, you don't even need to call the addListenerForSingleValueEvent method, because
Firebase Realtime-Database always retrieve all the children of the specified Reference.
So all you need is to add your Code line inside the foreach loop:

for (DataSnapshot d : dataSnapshot.getChildren()){


recordItems.add(new recordItems(iconGetter(d.child("Type").getValue().toString()),
d.child("Description").getValue().toString(),
d.child("Type").getValue().toString(),
d.child("Value").getValue().toString(),
d.child("Date").getValue().toString()));
}
And don't forget to notify yout adapter after adding all the new items to the list
with adapter.notifyDataSetChanged() and of course after you called initRecycler(); in the
beginning of this whole code.
ExpenseModel.java
public class ExpenseModel {

String Date,Description,Type,Value;

public ExpenseModel(){

public ExpenseModel(String date, String description, String type, String value) {

Date = date;
Description = description;
Type = type;
Value = value;
}

public String getDate() {

return Date;
}

public void setDate(String date) {

Date = date;
}

public String getDescription() {

return Description;
}

public void setDescription(String description) {

Description = description;
}

public String getType() {


return Type;
}

public void setType(String type) {


Type = type;
}

public String getValue() {


return Value;
}

public void setValue(String value) {


Value = value;
}
}
And The method should be something like this
final String date="15/03/2018";

private void updateValues()


{
if(rAuth !=null && rUser!=null)
{
final DatabaseReference rRef =
FirebaseDatabase.getInstance().getReference()
.child("Users")
.child("Transactions");

rRef.child("Expense").addChildEventListener(new ChildEventListener() {

@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {

ExpenseModel expenseModel = dataSnapshot.getValue(ExpenseModel.class);

assert expenseModel != null;


if(expenseModel.getDate().equals(date)){

// Add it to the recyclerView


// And call notifyDataSetChanged()

yourAdapter.add(expenseModel);
yourAdapter.notifyDataSetChanged();

}
}

@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {

@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {

@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {

@Override
public void onCancelled(DatabaseError databaseError) {
}
});

Вам также может понравиться