本文整理匯總了Java中com.google.firebase.database.ValueEventListener類的典型用法代碼示例。如果您正苦於以下問題:Java ValueEventListener類的具體用法?Java ValueEventListener怎麽用?Java ValueEventListener使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
ValueEventListener類屬於com.google.firebase.database包,在下文中一共展示了ValueEventListener類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: getMessages
import com.google.firebase.database.ValueEventListener; //導入依賴的package包/類
@Override
public void getMessages(final User u, final DatabaseCallback<List<Message>> dbCallback) {
ValueEventListener listener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
List<Message> messages = new ArrayList<>();
if (dataSnapshot.getChildrenCount() > 0) {
for (DataSnapshot s : dataSnapshot.getChildren()) {
messages.add(s.getValue(Message.class));
}
}
dbReference.child(u.getUid())
.child(MESSAGES).removeEventListener(this);
dbCallback.onSuccess(messages);
}
@Override
public void onCancelled(DatabaseError databaseError) {
dbCallback.onFailure(databaseError.toException());
}
};
dbReference.child(u.getUid())
.child(MESSAGES)
.addValueEventListener(listener);
}
示例2: getAllProductes
import com.google.firebase.database.ValueEventListener; //導入依賴的package包/類
public static List<Product> getAllProductes(){
final List<Product> Productes=new ArrayList<Product>();
// Attach a listener to read the data at our posts reference
ProductRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Product post = dataSnapshot.getValue(Product.class);
Productes.add(post);
}
@Override
public void onCancelled(DatabaseError databaseError) {
System.out.println("The read failed: " + databaseError.getCode());
}
});
return Productes;
}
示例3: push
import com.google.firebase.database.ValueEventListener; //導入依賴的package包/類
public Observable<Void> push() {
final DatabaseReference child = easyFirebaseDb.databaseReference.child(name);
for (Map.Entry<String, Object> entry : values.entrySet()) {
child.child(entry.getKey()).setValue(entry.getValue());
}
return Observable.create(new Observable.OnSubscribe<Void>() {
@Override
public void call(final Subscriber<? super Void> subscriber) {
child.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
subscriber.onNext(null);
subscriber.onCompleted();
}
@Override
public void onCancelled(DatabaseError databaseError) {
subscriber.onError(databaseError.toException());
}
});
}
});
}
示例4: canGetProfilePicUrl
import com.google.firebase.database.ValueEventListener; //導入依賴的package包/類
@Test
public void canGetProfilePicUrl() {
final String testUserForPic = "Test User for Pic";
final String url = "http://url.test.ch";
firebaseHelper.addOrUpdateUser(TEST_USER, testUserForPic);
firebaseHelper.setOrUpdateProfilePicUrl(testUserForPic, url);
firebaseHelper.getProfilePicUrl(testUserForPic, new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String fetchedUrl = (String) dataSnapshot.getValue();
Assert.assertTrue(url.equals(fetchedUrl));
}
@Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException();
}
});
}
示例5: notifyContactsOfConnectionChange
import com.google.firebase.database.ValueEventListener; //導入依賴的package包/類
public void notifyContactsOfConnectionChange(final boolean online, final boolean signoff) {
final String myEmail = getAuthUserEmail();
getMyContactsReference().addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
for (DataSnapshot child : snapshot.getChildren()) {
String email = child.getKey();
DatabaseReference reference = getOneContactReference(email, myEmail);
reference.setValue(online);
}
if (signoff){
FirebaseAuth.getInstance().signOut();
}
}
@Override
public void onCancelled(DatabaseError firebaseError) {
}
});
}
示例6: getObjects
import com.google.firebase.database.ValueEventListener; //導入依賴的package包/類
public <T> Observable<T> getObjects(final Query query, final Class<T> objectClass, final boolean useRootElement) {
return Observable.create(new Observable.OnSubscribe<T>() {
@Override
public void call(final Subscriber<? super T> subscriber) {
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(useRootElement) {
converter.convert(dataSnapshot, objectClass, subscriber);
} else {
for (DataSnapshot entry : dataSnapshot.getChildren()) {
converter.convert(entry, objectClass, subscriber);
}
}
subscriber.onCompleted();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
});
}
示例7: addUserRecords
import com.google.firebase.database.ValueEventListener; //導入依賴的package包/類
/**
* Add a new user
* @param userId - the user's ID
* @param name - the user's name
* @param email - the user's email
*/
public void addUserRecords(final String userId, final String name, final String email) {
mDatabase.child("users").child(userId).addListenerForSingleValueEvent(new ValueEventListener() {
/**
* Handle data change event
* @param snapshot - current database contents
*/
@Override
public void onDataChange(DataSnapshot snapshot) {
if (!snapshot.exists()) {
User user = new User(userId, name, email, 0);
mDatabase.child("users").child(userId).setValue(user);
}
}
/**
* Do nothing when the process is cancelled
* @param databaseError - Ignored error
*/
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
setMemberData();
}
示例8: testSubscribeThenUnsubscribeWithoutProblems
import com.google.firebase.database.ValueEventListener; //導入依賴的package包/類
@Test
public void testSubscribeThenUnsubscribeWithoutProblems()
throws InterruptedException {
DatabaseReference ref = IntegrationTestUtils.getRandomNode(masterApp) ;
ValueEventListener listener =
new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {}
@Override
public void onCancelled(DatabaseError error) {
fail("Should not be cancelled");
}
};
ValueEventListener listenerHandle = ref.addValueEventListener(listener);
ZombieVerifier.verifyRepoZombies(ref);
ref.removeEventListener(listenerHandle);
ZombieVerifier.verifyRepoZombies(ref);
ValueEventListener listenerHandle2 = ref.addValueEventListener(listener);
ZombieVerifier.verifyRepoZombies(ref);
ref.removeEventListener(listenerHandle2);
ZombieVerifier.verifyRepoZombies(ref);
}
示例9: observe
import com.google.firebase.database.ValueEventListener; //導入依賴的package包/類
private Observable<DataSnapshot> observe(final Query ref) {
return Observable.create(emitter -> {
final ValueEventListener listener =
ref.addValueEventListener(new ValueEventListener() {
@Override public void onDataChange(DataSnapshot dataSnapshot) {
emitter.onNext(dataSnapshot);
}
@Override public void onCancelled(DatabaseError databaseError) {
emitter.onError(new FirebaseException(databaseError.getMessage()));
}
});
emitter.setCancellable(() -> ref.removeEventListener(listener));
});
}
示例10: testAttachingListener
import com.google.firebase.database.ValueEventListener; //導入依賴的package包/類
@Test
public void testAttachingListener() {
DatabaseReference ref = IntegrationTestUtils.getRandomNode(masterApp);
ValueEventListener listener = ref.limitToLast(1)
.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
// No-op
}
@Override
public void onCancelled(DatabaseError error) {
}
});
assertNotNull(listener);
}
示例11: removeDetectiveProcess
import com.google.firebase.database.ValueEventListener; //導入依賴的package包/類
public void removeDetectiveProcess(ViewAppHolder.UserProfileViewHolder userProfileViewHolder,
Context context){
FirebaseHelper.getUserAccountSettings(USER_LINK_FIREBASE)
.child(Constants.DETECTION_MODE).setValue(Constants.USER_DETECTION_INACTIVE);
userProfileViewHolder.DETECTION_ICON.setImageTintList(
context.getResources().getColorStateList(R.color.safe_color));
FirebaseHelper.getUserFriendList(USER_LINK_FIREBASE)
.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()){
FirebaseHelper.getUserDetection(Utility.encodeUserEmail(dataSnapshot1.getValue(String.class)))
.child(USER_LINK_FIREBASE).removeValue();
}
FirebaseHelper.getUserLocation(USER_LINK_FIREBASE)
.removeEventListener(userDetectionLocationListener);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
示例12: displayCityStateCountry
import com.google.firebase.database.ValueEventListener; //導入依賴的package包/類
private void displayCityStateCountry() {
dbHelper.getDb().getReference( dbHelper.getUserPath() ).child( currUserID ).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for ( DataSnapshot field : dataSnapshot.getChildren() ) {
switch ( field.getKey() ) {
case "city":
cityEditText.setText( ( field.getValue() != null ) ? field.getValue().toString() : "" );
break;
case "state":
stateEditText.setText( ( field.getValue() != null ) ? field.getValue().toString() : "" );
break;
case "country":
countryEditText.setText( ( field.getValue() != null ) ? field.getValue().toString() : "" );
break;
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {}
});
}
示例13: removeFriendRequestAFromB
import com.google.firebase.database.ValueEventListener; //導入依賴的package包/類
private void removeFriendRequestAFromB(final String USER_A_EMAIL, final String USER_B_EMAIL){
FirebaseHelper.getUserFriendRequestList(Utility.encodeUserEmail(USER_B_EMAIL))
.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()){
if(dataSnapshot1.getValue().equals(USER_A_EMAIL)){
FirebaseHelper.getUserFriendRequestList(Utility.encodeUserEmail(USER_B_EMAIL))
.child(dataSnapshot1.getKey()).removeValue();
break;
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
示例14: listenUserAvailabilityCorrectlyAttachesListener
import com.google.firebase.database.ValueEventListener; //導入依賴的package包/類
@Test
public void listenUserAvailabilityCorrectlyAttachesListener() {
firebaseHelper.setUserAvailable(TEST_USER, false, true);
ValueEventListener listener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Assert.assertTrue(dataSnapshot.exists());
Assert.assertTrue((boolean)dataSnapshot.getValue());
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
};
firebaseHelper.listenUserAvailability(TEST_USER, false, listener);
}
示例15: testUpdateAfterSetLeafNodeWorks
import com.google.firebase.database.ValueEventListener; //導入依賴的package包/類
@Test
public void testUpdateAfterSetLeafNodeWorks() throws InterruptedException {
DatabaseReference ref = IntegrationTestUtils.getRandomNode(masterApp);
final Semaphore semaphore = new Semaphore(0);
final Map<String, Object> expected = new MapBuilder().put("a", 1L).put("b", 2L).build();
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
if (DeepEquals.deepEquals(snapshot.getValue(), expected)) {
semaphore.release();
}
}
@Override
public void onCancelled(DatabaseError error) {
}
});
ref.setValueAsync(42);
ref.updateChildrenAsync(expected);
TestHelpers.waitFor(semaphore);
}