umon
umon

Reputation: 245

Can i get value without using event listeners in firebase on android?

I'm making an Android application using new version of Google Firebase Realtime Database.

I am getting my datas with ValueEventListener and ChildEventListener when data changed/added/deleted/moved etc..

Now, i have a problem.

My data is :

"user 1": {
    "name":"abc"
}

and i have an a Button that named "getName".

I want to get value of "name" in user 1 data when i clicked the getName button.

    getName.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            bla bla..
        }
    });

Is it possible? Can i get value without using listeners?

i m waiting your helps. thank you.

Upvotes: 9

Views: 13771

Answers (4)

Danish Ajaib
Danish Ajaib

Reputation: 109

Of course, there is a way to achieve this, maybe not the way you want it. The first thing you have to do is to create a Model class for the 'user', let's call it User, it might go something like this:

package Model;

public class User{

private String name; // make sure this matches the one in Firebase


public String getName() {
    return name;
}

public void setName(String name) {
    this.name= name;
}



public User()
{
    //this non-argument constructor is necessary for Firebase to properly map all the variables.
}

public User(String name) {
    this.name= name;

    }
}

Now in a different activity, where you want to access the user's name.

getName.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
   databaseRef.child("users").child("user1").addListenerForSingleValueEvent(new ValueEventListener() {
   @Override
   public void onDataChange(DataSnapshot snapshot) {
      User user = snapshot.getValue(User.class);
      Log.i("user name", user.getName());




   }
   @Override
   public void onCancelled(FirebaseError firebaseError) {
   }
});

If your reference points to the 'users' node and you want to find a user with a specific name and access its other attributes, you can do this.

getName.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
   databaseRef.child("users").addListenerForSingleValueEvent(new ValueEventListener() {
   @Override
   public void onDataChange(DataSnapshot snapshot) {
      User user = snapshot.getValue(User.class);
      if(user.getName().equals("abc"))
      {
          Log.i("other data", user.getOtherData());
       }

   }
   @Override
   public void onCancelled(FirebaseError firebaseError) {
   }
});

Upvotes: 0

JP Ventura
JP Ventura

Reputation: 5732

Take a look at Tasks API framework:

@Override
public void onClick(View view) {
    getUser();
}

private Task<User> getUser(String id) {
    final TaskCompletionSource<User> tcs = new TaskCompletionSource();

    ref.child("users")
        .child(id)
        .addListenerForSingleValueEvent(new ValueEventListener() {

            @Override
            public void onCancelled(FirebaseError error) {
                tcs.setException(error.toException());
            }

            @Override
            public void onDataChange(DataSnapshot snapshot) {
                tcs.setResult(snapshot.getValue(User.class));
            }

        });

    return tcs.getTask();
}

Upvotes: 1

Izvorski
Izvorski

Reputation: 146

Not actually true it will change every time you change the data and it will probably call all of your activites which is pretty useless i don't even know why they made it that way....waiste of time.

Upvotes: 9

Parag Kadam
Parag Kadam

Reputation: 3850

Yes you can do that by using addListenerForSingleValueEvent. This will fetch the value only once unlike ValueEventListener and ChildEventListener. So your code will look something like this,

getName.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
       ref.addListenerForSingleValueEvent(new ValueEventListener() {
       @Override
       public void onDataChange(DataSnapshot snapshot) {
          // do some stuff once
       }
       @Override
       public void onCancelled(FirebaseError firebaseError) {
       }
});

Upvotes: 15

Related Questions