how to automatic fiil the child on firebase with Android studio - android

i am using android studio. i want to change "Checkin" on "room" child if date of checkin same with date of system. i have already take date from system. but how the code . for automatic change "Checkin" on "room" child. where "Checkin" on child booking same with date of system...

You can use childEventListener to get all child of a node in Firebase database, childEventListener will be triggered at the first time attached to a database reference and triggered again when child of the node of the reference added, changed, removed,... In your case, assume you have already defined Room class with those attributes you can add listener for you database as below:
String systemTime = "HH-MM-SS"; //your system time
DatabaseReference root = FirebaseDatabase.getInstance().getReference();
ChildEventListener childEventListener = new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String previousChildName) {
Room room = dataSnapshot.getValue(Room.class);
if(room.checkin.equals(systemTime))
{
//do something with the room
//then new value back
dataSnapshot.getRef().setValue(room);
}
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String previousChildName) {
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String previousChildName) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
};
root.child("booking").addChildEventListener(childEventListener);
Visit this link to get more information about working with list in firebase database https://firebase.google.com/docs/database/android/lists-of-data

Related

deleting data in firebase console is not deleting data from the arraylist where I stored the object value

I have an android project where I retrieve data from firebase. everything is working fine but when I delete object in firebase console, it does not reflect back in the app.
Here is the link of the image:
So, suppose I delete david hafiz child node in firebase, it does not delete in the app. I have tried a lot but can't find the correct way. I am new to android programming and I hope somebody can help me. Thank You.
Update
mDatabase = FirebaseDatabase.getInstance();
mReference = mDatabase.getReference().child("Students").child("Marks");
mReference.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Log.d("value", "" + dataSnapshot);
fetchData(dataSnapshot);
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
fetchData(dataSnapshot);
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
return view;
}
private void fetchData(DataSnapshot dataSnapshot) {
StudentData value = dataSnapshot.getValue(StudentData.class);
Log.v("StudentData Fragment", "" + dataSnapshot.getValue());
// Get an iterator.
Iterator<StudentData> ite = mMarksList.iterator();
while (ite.hasNext()) {
StudentData iteValue = ite.next();
if (iteValue.equals(value))
ite.remove();
}
mMarksList.add(value);
Collections.sort(mMarksList, new MarksComparator());
String title = mReference.getKey();
// specify an adapter
mAdapter = new MyAdapter(getContext(), mMarksList, title);
mAdapter.notifyDataSetChanged();
mRecyclerView.setAdapter(mAdapter);
}
There are four relevant methods in ``:
onChildAdded
onChildRemoved
onChildChanged
onChildMoved
You only implemented onChildAdded, which is called in two cases:
When you first attach the listener, onChildAdded is called for each existing child.
When a child is later added, onChildAdded is called for that child.
When you delete a node from the Firebase console, the onChildRemoved method is called. But since you left that method empty, your app doesn't do anything when you remove data from the console.
To make your app behave correctly, you'll need to implement onChildRemoved. Typically this involves finding the UI element matching the snapshot and removing it.
You can store keys in order to update or remove it e.g:
ArrayList<String> mKeys = new ArrayList<String>();
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String key = dataSnapshot.getKey();
mKeys.add(key);
adapter.notifyDataSetChanged();
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
String key = dataSnapshot.getKey();
int index = mKeys.indexOf(key);
mMarksList.remove(/*index*/); // or data
adapter.notifyDataSetChanged();
}
You might be interested how to update too.

How can I get value of this child?

It's not current id that user logged... it's other uid from other user when other user send a request.
From what I understand, you want to get to know whenever you receive a friend request. I see that you've created a Friend_req node and have each user's keys inside it. This is good. I assume that you do have a minimal grasp of Firebase and flattening of data.
With this assumption, my answer is that you need a childEventListener on the node that you need to track for friend requests. The childEventListener has a onChildAdded() method that downloads data whenever a new child is added to the code ( in your case, a new friend request ). Here's a basic implementation.
FirebaseDatabase.getInstance().getReference().child("Friend_req")
.child(yourUserKey).addChildEventListener(new ChildEventListener() {
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
//Get notified on friend request
}
#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) {
}
});

Firebase database not working with addChildEventListener

I'm using Firebase database and I'm retrieving data from it. When my database has data, addChildEventListener working well but when my database hasn't data, addChildEventListener not working. Bellow, this is my code:
mDatabase = FirebaseDatabase.getInstance().getReference().child("music");
public void getListMusicFromFirebase() {
mDatabase.addChildEventListener(this);
}
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
updateMusic(dataSnapshot);
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
updateMusic(dataSnapshot);
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
updateMusic(dataSnapshot);
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
private void updateMusic(DataSnapshot dataSnapshot) {
mListMusic.clear();
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
String title = snapshot.getValue(Music.class).getTitle();
String artist = snapshot.getValue(Music.class).getArtist();
String year = snapshot.getValue(Music.class).getYear();
String duration = snapshot.getValue(Music.class).getDuration();
String uri = snapshot.getValue(Music.class).getUri();
Music music = new Music(title, artist, year, duration, uri);
mListMusic.add(music);
}
if (mListMusic.size() > 0) {
bindingListMusic();
return;
}
mBinding.tvDataEmpty.setText("No data");
mDialog.dismiss();
}
I check my list. If size of it greater than 0, I will bind it to recyclerview and opposite, I will notify it haven't data, it working well when having data but when I delete all data from Firebase database, my code can't perform updateMusic() method.
Please help.
To initiate your listener you should have the required data in your firebase database. AddChildEvent listener requires a predefined parent where you will attach it on app launch(or whenever you want) . If there is no parent the listener will never be attached or initiated.
This will result in no response when you later add a parent and add childs to it. So, always make sure that the parent to which you adding childEventListener is already present and the value is not null.
Hope this helps.

Determine what child node's change fires "onChildChanged" method in firebase with android

Here is my firebase structure
Here is my code fore the firebase
final FirebaseDatabase database = FirebaseDatabase.getInstance();
bookingRef = database.getReference("booking");
query = bookingRef.orderByChild("studentID").equalTo("S00001");
query.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Booking myBooking = dataSnapshot.getValue(Booking.class);
addBookingRow(myBooking, dataSnapshot.getKey());
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
//here it is fired when the alarset value is changed
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
String tag = dataSnapshot.getKey();
removeBookingRow(tag);
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Each Booking node has several child nodes(?) which are classID, studentID, attended, alarmType
As you can see, I have got query for getting nodes having certain student ID.
Then, i have added addChildEventListener for the ordered nodes.
So, when any of the child nodes of the booking node changes, the onChildChanged method is fired, and it gives me the data snapshot for the booking node having child node change.
Here I want to know what child node changed. Like I want to know if the onChildChanged method is fired on the change of ClassID.
It is because I want to implement different behaviour based on change of each child node
Do i have to set some listener for each child node? or ..
Can you guys give me solutions?
I am developing in android studio
thanks

Firebase Android how to handle onChildAdded

How to handle onChildAdded event in firebase android.
below is my database json structure
{
message{
"id"=10;
"name"="xyz"
}
}
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
// how can I handle the particular child that has added
}
this method triggers when a new child is added to my reference node. But how can I get the value for the particular child with out typecasting
The child added is the one returned on the dataSnapshot. So, if you want to get it's key:
String myKey = dataSnapshot.getKey()
For the value:
String myKey = dataSnapshot.getValue(String.class)
For the value you need to define the type you are expecting. On this example I specified that a String is expected, but you can even cast the value to any object you are using (as long as the fields match on your object model and on the dataSnapshot):
User myUser = dataSnapshot.getValue(User.class)
For more details please go to the Firebase official documentation.
In reference, add your app firebase console url.
DatabaseReference myFirebaseRef = database.getReference("Your firbase path");
myFirebaseRef.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
if(dataSnapshot.hasChild("message")) {
for (DataSnapshot poly : dataSnapshot.child("message").getChildren()) {
String id=String.valueOf(poly.child("id").getValue);
String name=String.valueOf(poly.child("name").getValue);
}
}
}
#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) {
}
});
You will need a model class as per the child nodes and entities you have in your database.
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
ModelClass model = dataSnapshot.getValue(ModelClass.class)
//assuming this is used for list of data..
list.add(model);
}
Child Event Listener does not crash even if the child nodes are not available or not yet created dyanamically.You also won't need any loop for moving to the next node.
If you want to get just a specific child value then do this
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
if(dataSnapshot.hasChild("name")){
String name = dataSnapshot.child("name").getValue().toString();
}
}

Categories

Resources