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);
}
}
}
Related
I have a key and I want to retrieve value for it. This is my code:
public void afterTextChanged(Editable editable) {
// String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
//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())
{
for (DataSnapshot vale:ds1.getChildren())
{
Log.d("check",vale.toString());
vale.getKey();`
}
}
}
}
}
}
Supposing,value is of string type and key is "myKey"
public void afterTextChanged(Editable editable) {
// String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
//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())
{
for (DataSnapshot vale:ds1.getChildren())
{
Log.d("check",vale.toString());
String myValue=vale.child("myKey").getValue(String.class);
}
}
}
}
I have read many answers to this question, none solved my problem.
ValueEventListener dataListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
DataSnapshot ls = ds.child("/leafBox001");
DataSnapshot cs = ls.child("/currentState");
Log.i(TAG, "msg " + cs + ls + ds);
}
Log returns the following:
DataSnapshot { key = leafBox001, value = {lightSensor=6793, currentState=false} }
DataSnapshot { key = currentState, value = null }
DataSnapshot { key = leafBox001, value = null }
As you can see, when I try to access a child the values become null.
I also always get null when using getValue. Example of that code:
ValueEventListener dataListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
currentState = ds.child("/leafBox001").child("/currentState").getValue(Boolean.class);
Log.i(TAG, "msg " + cs + ls + ds);
}
I have tried every variation of the above code, always null.
RTD Structure
Your database structure is like this:
leafBox001
lightSensor:6793
currentState: false
then do the following:
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("leafBox001");
reference.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String sensor = dataSnapshot.child("lightSensor").getValue().toString();
String currentState = dataSnapshot.child("currentState").getValue().toString();
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
The snapshot is at the key leafBox001 then you will be able to access the child of that key.
I want to implement followers functionality in my android application. 'First' image refers to the list of user i followed. 'second' image have all ids of all the posts that users post and 'third' image havin post details. all i want to do is to load only the post of those users that i've followed.
What i am doing is, i get emails from followers node and by using indexed recyclerview i load the data but its not working because recyclerview is not getting dynamic reference. Any sugested solution ?
first
second
third
In order to achieve this, you need to query your database for three times like this:
FirebaseUser firebaseUser = firebaseAuth.getCurrentUser();
String userEmail = firebaseUser.getEmail();
String encodedUserEmail = userEmail.replace(".", ",");
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference userEmailFromFollowersRef = rootRef.child("followers").child(encodedUserEmail);
ValueEventListener eventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String followerEmail = ds.getKey();
DatabaseReference my_postsRef = rootRef.child("my_posts").child(followerEmail);
ValueEventListener valueEventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot dSnapshot : dataSnapshot.getChildren()) {
String postId = dSnapshot.getKey();
DatabaseReference postsRef = rootRef.child("posts").child(postId);
ValueEventListener vel = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot child : dataSnapshot.getChildren()) {
String postText = child.child("postText").getValue(String.class);
Log.d("TAG", postText);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
postsRef.addListenerForSingleValueEvent(vel);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
my_postsRef.addListenerForSingleValueEvent(valueEventListener);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
userEmailFromFollowersRef.addListenerForSingleValueEvent(eventListener);
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()){
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) {
}
});