本文整理汇总了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);
}