Reputation: 49
I read many threads regarding how to get data from Firebase database instance, but none of them worked for me.
My code in the activity:
public class Violations extends AppCompatActivity
{
TextView textView7;
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference dbref = database.getReference("save");
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_violations);
textView7 = findViewById(R.id.textView7);
dbref.addValueEventListener(new ValueEventListener()
{
ArrayList<String> Violations = new ArrayList<>();
@Override
public void onDataChange(DataSnapshot dataSnapshot)
{
for (DataSnapshot postSnapshot: dataSnapshot.getChildren())
{
Violations.add(postSnapshot.getValue().toString());
System.out.println(postSnapshot.getValue().toString());
}
for(int i=0; i < Violations.size(); i++)
{
textView7.setText(textView7.getText() + Violations.get(i) + System.lineSeparator());
}
}
@Override
public void onCancelled(DatabaseError databaseError) {}
});
}
}
There is no error in there, but no data displays. I am pretty sure, my problem is connecting to the right instance and retrieving the data.
Firebase data, are like this:
Can someone please help me in there?
Thanks you in advance.
Upvotes: 3
Views: 988
Reputation: 138869
I cannot see in your database schema o reference named save
but I see one named You violate your own speed limit with
, which mush be used in your reference in order to be able to get data from the database. So to solve this, please use the following lines of code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference ref = rootRef.child("You violate your own speed limit with");
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
ArrayList<String> violations = new ArrayList<>();
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String speed = ds.getValue(String.class);
violations.add(speed);
Log.d(TAG, speed);
}
//Do what you need to do with y our violations list
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.d(TAG, databaseError.getMessage());
}
};
ref.addListenerForSingleValueEvent(valueEventListener);
The result in your logcat will be all those values:
3 km / h at time: ...
3 km / h at time: ...
//an so on
Upvotes: 1
Reputation: 48
Try to use addListenerForSingleValue()
instead of addValueEventListener()
. Hope it helps. And change your firebase link save
to you violate your own speed limit with
Upvotes: 1
Reputation: 16976
Initialize the Firebase database & the DatabaseReference
inside onCreate()
method:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_violations);
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference dbref = database.getReference("save");
...
..
Also, the reference you are getting is save
but the Firebase database shows that is : you violate ...
. You may try changing the name to save
or getting the right data : you violate ...
.
Upvotes: 2