Java Code Examples for com.google.firebase.database.Query#addValueEventListener()
The following examples show how to use
com.google.firebase.database.Query#addValueEventListener() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: detailActivity.java From Doctorave with MIT License | 6 votes |
public void deleteFromFirebase(final String id){ Query hekkQuery = mDatabaseReference; hekkQuery.addValueEventListener(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { dataSnapshot.child(id).getRef().removeValue() .addOnSuccessListener(new OnSuccessListener<Void>() { @Override public void onSuccess(Void aVoid) { deletePhotoFromStorage(id); } }); } @Override public void onCancelled(DatabaseError databaseError) { } }); /////this is for firebase storage Task<Void> task = mStorageReference.child(String.valueOf(id).concat("_Image")).delete(); }
Example 2
Source File: QueryActivity.java From snippets-android with Apache License 2.0 | 6 votes |
public void basicQueryValueListener() { String myUserId = getUid(); Query myTopPostsQuery = databaseReference.child("user-posts").child(myUserId) .orderByChild("starCount"); // [START basic_query_value_listener] // My top posts by number of stars myTopPostsQuery.addValueEventListener(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { for (DataSnapshot postSnapshot: dataSnapshot.getChildren()) { // TODO: handle the post } } @Override public void onCancelled(DatabaseError databaseError) { // Getting Post failed, log a message Log.w(TAG, "loadPost:onCancelled", databaseError.toException()); // ... } }); // [END basic_query_value_listener] }
Example 3
Source File: FirestackDatabase.java From react-native-firestack with MIT License | 6 votes |
public void addValueEventListener(final String name, final ReadableArray modifiers) { final FirestackDBReference self = this; mValueListener = new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { self.handleDatabaseEvent("value", mPath, dataSnapshot); } @Override public void onCancelled(DatabaseError error) { self.handleDatabaseError("value", mPath, error); } }; Query ref = this.getDatabaseQueryAtPathAndModifiers(modifiers); ref.addValueEventListener(mValueListener); this.setListeningTo(mPath, "value"); }
Example 4
Source File: ReadFuture.java From firebase-android-sdk with Apache License 2.0 | 5 votes |
private ReadFuture( final Query ref, final CompletionCondition condition, final boolean ignoreFirstNull) { semaphore = new Semaphore(0); this.valueEventListener = new ValueEventListener() { @Override public void onDataChange(DataSnapshot snapshot) { if (ignoreFirstNull && events.size() == 0 && snapshot.getValue() == null) { return; } events.add(new EventRecord(snapshot, Event.EventType.VALUE, null)); try { if (condition.isComplete(events)) { ref.removeEventListener(valueEventListener); finish(); } } catch (Exception e) { exception = e; finish(); } } @Override public void onCancelled(DatabaseError error) { wasCancelled = true; finish(); } }; ref.addValueEventListener(this.valueEventListener); }
Example 5
Source File: addPatientActivity.java From Doctorave with MIT License | 5 votes |
public void updateInFirebase(final String id, final patientsInfo info){ Query hekkQuery = mDatabaseReference; hekkQuery.addValueEventListener(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { dataSnapshot.child(id).getRef().setValue(info); } @Override public void onCancelled(DatabaseError databaseError) { } }); }
Example 6
Source File: OrderByTestIT.java From firebase-admin-java with Apache License 2.0 | 5 votes |
@Test public void testQueriesOnLeafNodes() throws InterruptedException, ExecutionException, TestFailure, TimeoutException { DatabaseReference ref = IntegrationTestUtils.getRandomNode(masterApp) ; final Semaphore semaphore = new Semaphore(0); new WriteFuture(ref, "leaf-node").timedGet(); final List<DataSnapshot> snapshots = new ArrayList<>(); Query query = ref.orderByChild("foo").limitToLast(1); final ValueEventListener listener = query.addValueEventListener( new ValueEventListener() { @Override public void onDataChange(DataSnapshot snapshot) { snapshots.add(snapshot); semaphore.release(); } @Override public void onCancelled(DatabaseError error) { Assert.fail(); } }); TestHelpers.waitFor(semaphore); Assert.assertEquals(1, snapshots.size()); Assert.assertNull(snapshots.get(0).getValue()); // cleanup TestHelpers.waitForRoundtrip(ref); ref.removeEventListener(listener); }
Example 7
Source File: OrderByTestIT.java From firebase-admin-java with Apache License 2.0 | 5 votes |
@Test public void testServerRespectsKeyIndex() throws InterruptedException, ExecutionException, TimeoutException, TestFailure { List<DatabaseReference> refs = IntegrationTestUtils.getRandomNode(masterApp, 2); DatabaseReference writer = refs.get(0); DatabaseReference reader = refs.get(1); Map<String, Object> initial = MapBuilder.of("a", 1, "b", 2, "c", 3); // If the server doesn't respect the index, it will send down limited data, but with no // offset, so the expected and actual data don't match. Query query = reader.orderByKey().startAt("b").limitToFirst(2); new WriteFuture(writer, initial).timedGet(); final List<String> actualChildren = new ArrayList<>(); final Semaphore semaphore = new Semaphore(0); ValueEventListener valueListener = query.addValueEventListener( new ValueEventListener() { @Override public void onDataChange(DataSnapshot snapshot) { for (DataSnapshot child : snapshot.getChildren()) { actualChildren.add(child.getKey()); } semaphore.release(); } @Override public void onCancelled(DatabaseError error) { Assert.fail(); } }); TestHelpers.waitFor(semaphore); Assert.assertEquals(ImmutableList.of("b", "c"), actualChildren); // cleanup reader.removeEventListener(valueListener); }
Example 8
Source File: ReadFuture.java From firebase-admin-java with Apache License 2.0 | 5 votes |
private ReadFuture( final Query ref, final CompletionCondition condition, final boolean ignoreFirstNull) { semaphore = new Semaphore(0); this.valueEventListener = new ValueEventListener() { @Override public void onDataChange(DataSnapshot snapshot) { if (ignoreFirstNull && events.size() == 0 && snapshot.getValue() == null) { return; } events.add(new EventRecord(snapshot, Event.EventType.VALUE, null)); try { if (condition.isComplete(events)) { ref.removeEventListener(valueEventListener); finish(); } } catch (Exception e) { exception = e; finish(); } } @Override public void onCancelled(DatabaseError error) { wasCancelled = true; finish(); } }; ref.addValueEventListener(this.valueEventListener); }
Example 9
Source File: loginActivity.java From Doctorave with MIT License | 4 votes |
private void createDoctorTable(final String doctorUsername) { Query hekkQuery = mDatabaseReference.orderByChild(charUtility.filterString(doctorUsername)); hekkQuery.addValueEventListener(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { for (DataSnapshot appleSnapshot: dataSnapshot.getChildren()) { for(DataSnapshot appleSnapshot1: appleSnapshot.getChildren()){ if(appleSnapshot1.getKey().equals(charUtility.filterString(doctorUsername))){ String firstname = (String) appleSnapshot.child(charUtility.filterString(doctorUsername)).child("doctorInfo").child("firstName").getValue(); String lastname = (String) appleSnapshot.child(charUtility.filterString(doctorUsername)).child("doctorInfo").child("lastName").getValue(); String userphone = (String) appleSnapshot.child(charUtility.filterString(doctorUsername)).child("doctorInfo").child("phone").getValue(); String useremail = (String) appleSnapshot.child(charUtility.filterString(doctorUsername)).child("doctorInfo").child("email").getValue(); String userpassword = (String) appleSnapshot.child(charUtility.filterString(doctorUsername)).child("doctorInfo").child("password").getValue(); String title = (String) appleSnapshot.child(charUtility.filterString(doctorUsername)).child("doctorInfo").child("title").getValue(); String doctorInstitute = (String) appleSnapshot.child(charUtility.filterString(doctorUsername)).child("doctorInfo").child("instituteName").getValue(); String doctorInstituteAddress = (String) appleSnapshot.child(charUtility.filterString(doctorUsername)).child("doctorInfo").child("instituteAddress").getValue(); ContentValues cv = new ContentValues(); cv.put(doctorEntry.COLUMN_PUSHID, appleSnapshot.getKey()); cv.put(doctorEntry.COLUMN_NAME, firstname.concat("@@@@").concat(lastname)); cv.put(doctorEntry.COLUMN_PHONE_NUMBER, userphone); cv.put(doctorEntry.COLUMN_EMAIL, useremail); cv.put(doctorEntry.COLUMN_PASSWORD, userpassword); cv.put(doctorEntry.COLUMN_TITLE, title); cv.put(doctorEntry.COLUMN_INSTITUTE, doctorInstitute); cv.put(doctorEntry.COLUMN_IMAGE, (byte[]) null); cv.put(doctorEntry.COLUMN_INSTITUTE_ADDRESS, doctorInstituteAddress); Uri uri = getContentResolver().insert(doctorEntry.CONTENT_URI, cv); if(uri == null){ progressDialog.dismiss(); return; }else { loginSuccessfull(useremail, appleSnapshot.getKey()); } } } } } @Override public void onCancelled(DatabaseError databaseError) { progressDialog.dismiss(); } }); }
Example 10
Source File: OrderByTestIT.java From firebase-admin-java with Apache License 2.0 | 4 votes |
@Test public void testUseFallbackThenDefineIndex() throws InterruptedException, ExecutionException, TimeoutException, TestFailure, IOException { DatabaseReference writer = IntegrationTestUtils.getRandomNode(masterApp) ; DatabaseReference readerReference = FirebaseDatabase.getInstance(masterApp).getReference(); DatabaseReference reader = readerReference.child(writer.getPath().toString()); Map<String, Object> foo1 = TestHelpers.fromJsonString( "{ " + "\"a\": {\"order\": 2, \"foo\": 1}, " + "\"b\": {\"order\": 0}, " + "\"c\": {\"order\": 1, \"foo\": false}, " + "\"d\": {\"order\": 3, \"foo\": \"hello\"} }"); new WriteFuture(writer, foo1).timedGet(); final List<DataSnapshot> snapshots = new ArrayList<>(); final Semaphore semaphore = new Semaphore(0); Query query = reader.orderByChild("order").limitToLast(2); final ValueEventListener listener = query.addValueEventListener( new ValueEventListener() { @Override public void onDataChange(DataSnapshot snapshot) { snapshots.add(snapshot); semaphore.release(); } @Override public void onCancelled(DatabaseError error) { Assert.fail(); } }); TestHelpers.waitFor(semaphore); Assert.assertEquals(1, snapshots.size()); Map<String, Object> expected = MapBuilder.of( "d", MapBuilder.of("order", 3L, "foo", "hello"), "a", MapBuilder.of("order", 2L, "foo", 1L)); Assert.assertEquals(expected, snapshots.get(0).getValue()); uploadRules(masterApp, formatRules(reader, "{ \".indexOn\": \"order\" }")); Map<String, Object> fooE = TestHelpers.fromJsonString("{\"order\": 1.5, \"foo\": true}"); new WriteFuture(writer.child("e"), fooE).timedGet(); TestHelpers.waitForRoundtrip(reader); Map<String, Object> fooF = TestHelpers.fromJsonString("{\"order\": 4, \"foo\": {\"bar\": \"baz\"}}"); new WriteFuture(writer.child("f"), fooF).timedGet(); TestHelpers.waitForRoundtrip(reader); TestHelpers.waitFor(semaphore); Assert.assertEquals(2, snapshots.size()); Map<String, Object> expected2 = new MapBuilder() .put( "f", new MapBuilder() .put("order", 4L) .put("foo", MapBuilder.of("bar", "baz")) .build()) .put("d", MapBuilder.of("order", 3L, "foo", "hello")) .build(); Assert.assertEquals(expected2, snapshots.get(1).getValue()); // cleanup TestHelpers.waitForRoundtrip(reader); reader.removeEventListener(listener); }
Example 11
Source File: OrderByTestIT.java From firebase-admin-java with Apache License 2.0 | 4 votes |
@Test public void testUpdatesForUnindexedQuery() throws InterruptedException, ExecutionException, TestFailure, TimeoutException { List<DatabaseReference> refs = IntegrationTestUtils.getRandomNode(masterApp, 2); final DatabaseReference reader = refs.get(0); final DatabaseReference writer = refs.get(1); final List<DataSnapshot> snapshots = new ArrayList<>(); Map<String, Object> value = new HashMap<>(); value.put("one", new MapBuilder().put("index", 1).put("value", "one").build()); value.put("two", new MapBuilder().put("index", 2).put("value", "two").build()); value.put("three", new MapBuilder().put("index", 3).put("value", "three").build()); new WriteFuture(writer, value).timedGet(); final Semaphore semaphore = new Semaphore(0); Query query = reader.orderByChild("index").limitToLast(2); final ValueEventListener listener = query.addValueEventListener( new ValueEventListener() { @Override public void onDataChange(DataSnapshot snapshot) { snapshots.add(snapshot); semaphore.release(); } @Override public void onCancelled(DatabaseError error) { Assert.fail(); } }); TestHelpers.waitFor(semaphore); Assert.assertEquals(1, snapshots.size()); Map<String, Object> expected1 = new HashMap<>(); expected1.put("two", new MapBuilder().put("index", 2L).put("value", "two").build()); expected1.put("three", new MapBuilder().put("index", 3L).put("value", "three").build()); Assert.assertEquals(expected1, snapshots.get(0).getValue()); // update child which should trigger value event writer.child("one/index").setValueAsync(4); TestHelpers.waitFor(semaphore); Assert.assertEquals(2, snapshots.size()); Map<String, Object> expected2 = new HashMap<>(); expected2.put("three", new MapBuilder().put("index", 3L).put("value", "three").build()); expected2.put("one", new MapBuilder().put("index", 4L).put("value", "one").build()); Assert.assertEquals(expected2, snapshots.get(1).getValue()); // cleanup TestHelpers.waitForRoundtrip(reader); reader.removeEventListener(listener); }
Example 12
Source File: OrderByTestIT.java From firebase-admin-java with Apache License 2.0 | 4 votes |
@Test public void testServerRespectsValueIndex() throws InterruptedException, ExecutionException, TimeoutException, TestFailure, IOException { List<DatabaseReference> refs = IntegrationTestUtils.getRandomNode(masterApp, 2); DatabaseReference writer = refs.get(0); DatabaseReference reader = refs.get(1); final String indexRule = "{ \".indexOn\": \".value\"}"; String rules = formatRules(writer, indexRule); uploadRules(masterApp, rules); Map<String, Object> initial = MapBuilder.of("a", 1, "c", 2, "b", 3); // If the server doesn't respect the index, it will send down limited data, but with no // offset, so the expected and actual data don't match. Query query = reader.orderByValue().startAt(2).limitToFirst(2); new WriteFuture(writer, initial).timedGet(); final List<String> actualChildren = new ArrayList<>(); final Semaphore semaphore = new Semaphore(0); ValueEventListener valueListener = query.addValueEventListener( new ValueEventListener() { @Override public void onDataChange(DataSnapshot snapshot) { for (DataSnapshot child : snapshot.getChildren()) { actualChildren.add(child.getKey()); } semaphore.release(); } @Override public void onCancelled(DatabaseError error) { Assert.fail(); } }); TestHelpers.waitFor(semaphore); Assert.assertEquals(ImmutableList.of("c", "b"), actualChildren); // cleanup reader.removeEventListener(valueListener); }