Firebase Database onChildChanged() not triggered after 1st update - android

I added data in Firebase Realtime Database and set addChildEventListener using below code.
private DatabaseReference mFirebaseDatabaseRef;
mFirebaseDatabaseRef = FirebaseDatabase.getInstance().getReference("user").child("details");
for (int i = 0; i < result.size(); i++) {
String id = mFirebaseDatabaseRef.push().getKey();
String name = result.get(i).getName();
Details details = new Details();
appDetails.setName(name);
appDetails.setLabel(result.get(i).getLabel());
appDetails.setIconbase64(result.get(i).getIconbase64());
mFirebaseDatabaseRef.child(id).setValue(details );
}
addEventListener();
Add Child event listener method.
private void addEventListener() {
mFirebaseDatabaseRef.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
Log.d("Realtime", "s");
}
#Override
public void onChildChanged(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
Log.d("Realtime", "s");
Toast.makeText(mContext, "Child Changed", Toast.LENGTH_LONG).show();
}
#Override
public void onChildRemoved(#NonNull DataSnapshot dataSnapshot) {
Log.d("Realtime", "s");
}
#Override
public void onChildMoved(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
Log.d("Realtime", "s");
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Log.d("Realtime", "s");
}
});
}
Now when I change data in Database, onChildChanged of addChildEventListener is called only for 1st time. but when I again change data in Datbase, no onChildChanged triggered.
I want to trigger onChildChanged every time when I update Data in
Datbase.

you need to call your data in onChildAdded() method instead of onChildChanged() method
private void addEventListener() {
mFirebaseDatabaseRef.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
Log.d("Realtime", "s");
Toast.makeText(mContext, "Child Changed", Toast.LENGTH_LONG).show();
}
#Override
public void onChildChanged(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
Log.d("Realtime", "s");
Toast.makeText(mContext, "Child Changed", Toast.LENGTH_LONG).show();
}
#Override
public void onChildRemoved(#NonNull DataSnapshot dataSnapshot) {
Log.d("Realtime", "s");
}
#Override
public void onChildMoved(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
Log.d("Realtime", "s");
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Log.d("Realtime", "s");
}
});
}

I found the Solution,
I restart my app for some reason that is the main issue.
So I again add addChildEventListener in onCreate method and all the thing is working as expected.

Related

Problem in implementing ValueEventListeners

In the below code, I'm doing is what I am checking whether EventsRef dataSnapshot exists or not, if dataSnapshot exists = I am getting some values and storing it in a list and passing that list values to recyclerView method ,in that it displays those values, and my problem is if 2 users in online and both of them getting displayed those details , if user1 removes that reference from firebase database by deleting that event, user2 getting error and getting out of the application . I used child event listener as in the below code but it doesn't works as well . and if i use if else condtions inside the child event listener inside on child Added it also doesn't works , So please help me to solve this both issues .Thanks in advance.
EventsRef.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot)
{
if (dataSnapshot.exists())
{
EventsRef.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(#NonNull DataSnapshot dataSnapshot, #Nullable String s)
{
EventsRef.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot)
{
if (dataSnapshot.exists())
{
for (DataSnapshot ds : dataSnapshot.getChildren())
{
String eventId = ds.child("Eid").getValue(String.class);
String gender = ds.child("Gender").getValue(String.class);
String serveCount = ds.child("Serve").getValue(String.class);
list.add(eventId);
list2.add(gender);
list3.add(serveCount);
}
if (list.size() != 0)
{
for (int i = 0; i < list.size(); i++)
{
RecyclerView(list.get(i), list2.get(i), list3.get(i));
}
}
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
#Override
public void onChildChanged(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
}
#Override
public void onChildRemoved(#NonNull DataSnapshot dataSnapshot)
{
Toast.makeText(WorkerMyEventActivity.this, "Event removed", Toast.LENGTH_SHORT).show();
}
#Override
public void onChildMoved(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
else
{
progressDialog.dismiss();
TextView txt = (TextView) findViewById(R.id.txt);
txt.setVisibility(View.VISIBLE);
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});

event listener doesn't trigger when data changes firebase android

As i told you in question when some data changes in firebase realtime databese nothing heppens on datachange() method.
I tried every eventlistener child,value and single but none of them worked.Please help me with that problem i am trying to solve it about 5 hours.
public void StatusTextCheck(){
typing_status.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
Toast.makeText(MainActivity.this,"Event listener triggered.",Toast.LENGTH_LONG).show();
TypingStatus type = dataSnapshot.getValue(TypingStatus.class);
}
#Override
public void onChildChanged(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
}
#Override
public void onChildRemoved(#NonNull DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
Here is the reference define:
private DatabaseReference user_dbref,
message_dbref,typing_status_dbref,typing_status;
user_dbref = database.getReference("Users");
message_dbref = database.getReference("Message");
typing_status = database.getReference("Typing_Status");
#PradyumanDixit

Delete a child from firebase android?

mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
#Override
public boolean onMarkerClick(com.google.android.gms.maps.model.Marker marker) {
// Toast.makeText(Map.this, marker.getPosition()+"", Toast.LENGTH_SHORT).show();
Query query = databaseReferenceMarkers.orderByChild("lat").equalTo(marker.getPosition().latitude);
query.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
Toast.makeText(Map.this, "test", Toast.LENGTH_SHORT).show();
dataSnapshot.getRef().removeValue();
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
return true;
}
});
Why am I not able to delete the marker?
onChildRemoved() -- no response.
The problem is that you're adding a value change listener, so onChildRemoved() will only be called once you remove the child, but you're removing it in the callback function.
Try it like this:
Query query = databaseReferenceMarkers.orderByChild("lat").equalTo(marker.getPosition().latitude);
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot child: dataSnapshot.getChildren()) { child.getRef().removeValue(); }
}
#Override
public void onCancelled(DatabaseError databaseError) {
Log.e(TAG, "onCancelled", databaseError.toException());
}
});
The query returns a collection of datasnapshots where there is a match. Then you'd need to iterate over them deleting every one of them.

How to get User id from Firebase in android?

I have Done this:
adapter.getRef(currentPosition).addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
adapterKey = dataSnapshot.getChildren().toString();
Log.i("adapterKey",adapterKey);
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
I want to retrieve the Id's generated by firebase in order to access the inner data to EndUsers node. How can I do that?
Try like this
for(DataSnapshot snapshot:dataSnapshot.getChildren()){
//snapshot.getKey()
//snapshot.getValue()
}
DatabaseReference mDatabaseReference; //you must initialize it
mDatabaseReference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot child : dataSnapshot.getChildren()) {
Log.i(TAG, "the key is : " + child.getKey());
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});

ValueEventListener Not Called Inside ChildEventListener

I have a ChildEventListener to iterate through a list of children in a Firebase node. Each of those children is a child of another node, and I am trying to access them using a ValueEventListener inside of my ChildEventListener. The ValueEventListener won't call at all. I have a print statement in the onDataChange method and it doesn't print.
databaseReference.addOnChildEventListener(new ChildEventListener(){
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
databaseReference.addListenerForSingleValueEvent(new ValueEventListener(){
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
System.out.println("PRINT");
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Theres more to databaseReference before I call the addOnChild... or addListener... but I don't know why it won't print the statement.

Categories

Resources