Skip to content Skip to sidebar Skip to footer

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/ad

Solution 1:

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(newView.OnClickListener() {
    @OverridepublicvoidonClick(View v) {
       ref.addListenerForSingleValueEvent(newValueEventListener() {
       @OverridepublicvoidonDataChange(DataSnapshot snapshot) {
          // do some stuff once
       }
       @OverridepublicvoidonCancelled(FirebaseError firebaseError) {
       }
});

Solution 2:

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.

Solution 3:

Take a look at Tasks API framework:

@OverridepublicvoidonClick(View view) {
    getUser();
}

privateTask<User> getUser(String id) {
    final TaskCompletionSource<User> tcs = newTaskCompletionSource();

    ref.child("users")
        .child(id)
        .addListenerForSingleValueEvent(newValueEventListener() {

            @OverridepublicvoidonCancelled(FirebaseError error) {
                tcs.setException(error.toException());
            }

            @OverridepublicvoidonDataChange(DataSnapshot snapshot) {
                tcs.setResult(snapshot.getValue(User.class));
            }

        });

    return tcs.getTask();
}

Solution 4:

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;

publicclassUser{

privateString name; // make sure this matches the one in FirebasepublicStringgetName() {
    return name;
}

publicvoidsetName(String name) {
    this.name= name;
}



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

publicUser(String name) {
    this.name= name;

    }
}

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

getName.setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(View v) {
   databaseRef.child("users").child("user1").addListenerForSingleValueEvent(newValueEventListener() {
   @OverridepublicvoidonDataChange(DataSnapshot snapshot) {
      User user = snapshot.getValue(User.class);
      Log.i("user name", user.getName());




   }
   @OverridepublicvoidonCancelled(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(newView.OnClickListener() {
@OverridepublicvoidonClick(View v) {
   databaseRef.child("users").addListenerForSingleValueEvent(newValueEventListener() {
   @OverridepublicvoidonDataChange(DataSnapshot snapshot) {
      User user = snapshot.getValue(User.class);
      if(user.getName().equals("abc"))
      {
          Log.i("other data", user.getOtherData());
       }

   }
   @OverridepublicvoidonCancelled(FirebaseError firebaseError) {
   }
});

Post a Comment for "Can I Get Value Without Using Event Listeners In Firebase On Android?"