How to get child object's value inside a key from a list of data (using childeventlistener in firebase:...
up vote
1
down vote
favorite
Ok so I've been looking for related articles regarding this, I've made a few experiments but I can't understand why I can't still get the values of note, date_time and vaccine objects... I'm planning on putting them in a ListView and I already got the key from the list of data using ChildEventListener
lastlastref = myRef.child(babyid).child("baby_features").child("immunization_records");
lastlastref.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String string = dataSnapshot.getValue(String.class);
}
@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) {
}
});
and then I've tried using EventListener to get the values inside of it
lastlastref = myRef.child(babyid).child("baby_features").child("immunization_records");
lastlastref.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String string = dataSnapshot.getValue(String.class);
DatabaseReference newRef = lastlastref.child(string);
newRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
note = snapshot.child("note").getValue(String.class);
vaccine = snapshot.child("vaccine").getValue(String.class);
timestamp = snapshot.child("date_time").getValue(String.class);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
arrayList.add(vaccine + "" + timestamp + "" + note);
adapter.notifyDataSetChanged();
}
@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) {
}
});
But the app still crashes and logcat says "Can't pass null for argument 'pathString' in child()"
android firebase firebase-realtime-database
add a comment |
up vote
1
down vote
favorite
Ok so I've been looking for related articles regarding this, I've made a few experiments but I can't understand why I can't still get the values of note, date_time and vaccine objects... I'm planning on putting them in a ListView and I already got the key from the list of data using ChildEventListener
lastlastref = myRef.child(babyid).child("baby_features").child("immunization_records");
lastlastref.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String string = dataSnapshot.getValue(String.class);
}
@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) {
}
});
and then I've tried using EventListener to get the values inside of it
lastlastref = myRef.child(babyid).child("baby_features").child("immunization_records");
lastlastref.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String string = dataSnapshot.getValue(String.class);
DatabaseReference newRef = lastlastref.child(string);
newRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
note = snapshot.child("note").getValue(String.class);
vaccine = snapshot.child("vaccine").getValue(String.class);
timestamp = snapshot.child("date_time").getValue(String.class);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
arrayList.add(vaccine + "" + timestamp + "" + note);
adapter.notifyDataSetChanged();
}
@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) {
}
});
But the app still crashes and logcat says "Can't pass null for argument 'pathString' in child()"
android firebase firebase-realtime-database
This means thatbabyid
isnull
, which is not allowed in calls tochild()
. You can easily verify the value ofbabyid
by running the code in a debugger, and putting a breakpoint on the line withlastlastref = myRef.child(babyid)...
.
– Frank van Puffelen
Nov 20 at 4:34
ok, fixed the null error but it still won't get the children object values... could my code be lacking something?
– Jackie Chan
Nov 20 at 16:03
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
Ok so I've been looking for related articles regarding this, I've made a few experiments but I can't understand why I can't still get the values of note, date_time and vaccine objects... I'm planning on putting them in a ListView and I already got the key from the list of data using ChildEventListener
lastlastref = myRef.child(babyid).child("baby_features").child("immunization_records");
lastlastref.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String string = dataSnapshot.getValue(String.class);
}
@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) {
}
});
and then I've tried using EventListener to get the values inside of it
lastlastref = myRef.child(babyid).child("baby_features").child("immunization_records");
lastlastref.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String string = dataSnapshot.getValue(String.class);
DatabaseReference newRef = lastlastref.child(string);
newRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
note = snapshot.child("note").getValue(String.class);
vaccine = snapshot.child("vaccine").getValue(String.class);
timestamp = snapshot.child("date_time").getValue(String.class);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
arrayList.add(vaccine + "" + timestamp + "" + note);
adapter.notifyDataSetChanged();
}
@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) {
}
});
But the app still crashes and logcat says "Can't pass null for argument 'pathString' in child()"
android firebase firebase-realtime-database
Ok so I've been looking for related articles regarding this, I've made a few experiments but I can't understand why I can't still get the values of note, date_time and vaccine objects... I'm planning on putting them in a ListView and I already got the key from the list of data using ChildEventListener
lastlastref = myRef.child(babyid).child("baby_features").child("immunization_records");
lastlastref.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String string = dataSnapshot.getValue(String.class);
}
@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) {
}
});
and then I've tried using EventListener to get the values inside of it
lastlastref = myRef.child(babyid).child("baby_features").child("immunization_records");
lastlastref.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String string = dataSnapshot.getValue(String.class);
DatabaseReference newRef = lastlastref.child(string);
newRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
note = snapshot.child("note").getValue(String.class);
vaccine = snapshot.child("vaccine").getValue(String.class);
timestamp = snapshot.child("date_time").getValue(String.class);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
arrayList.add(vaccine + "" + timestamp + "" + note);
adapter.notifyDataSetChanged();
}
@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) {
}
});
But the app still crashes and logcat says "Can't pass null for argument 'pathString' in child()"
android firebase firebase-realtime-database
android firebase firebase-realtime-database
edited Nov 20 at 17:29
Frank van Puffelen
223k26364390
223k26364390
asked Nov 20 at 4:18
Jackie Chan
104
104
This means thatbabyid
isnull
, which is not allowed in calls tochild()
. You can easily verify the value ofbabyid
by running the code in a debugger, and putting a breakpoint on the line withlastlastref = myRef.child(babyid)...
.
– Frank van Puffelen
Nov 20 at 4:34
ok, fixed the null error but it still won't get the children object values... could my code be lacking something?
– Jackie Chan
Nov 20 at 16:03
add a comment |
This means thatbabyid
isnull
, which is not allowed in calls tochild()
. You can easily verify the value ofbabyid
by running the code in a debugger, and putting a breakpoint on the line withlastlastref = myRef.child(babyid)...
.
– Frank van Puffelen
Nov 20 at 4:34
ok, fixed the null error but it still won't get the children object values... could my code be lacking something?
– Jackie Chan
Nov 20 at 16:03
This means that
babyid
is null
, which is not allowed in calls to child()
. You can easily verify the value of babyid
by running the code in a debugger, and putting a breakpoint on the line with lastlastref = myRef.child(babyid)...
.– Frank van Puffelen
Nov 20 at 4:34
This means that
babyid
is null
, which is not allowed in calls to child()
. You can easily verify the value of babyid
by running the code in a debugger, and putting a breakpoint on the line with lastlastref = myRef.child(babyid)...
.– Frank van Puffelen
Nov 20 at 4:34
ok, fixed the null error but it still won't get the children object values... could my code be lacking something?
– Jackie Chan
Nov 20 at 16:03
ok, fixed the null error but it still won't get the children object values... could my code be lacking something?
– Jackie Chan
Nov 20 at 16:03
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
You set up your reference to:
lastlastref = myRef.child(babyid).child("baby_features").child("immunization_records");
So each child node under this location is a JSON object like this:
{
"date_time": "November/16/2018...",
"note": "hhjj...",
"vaccine": "Measles"
}
But in your onChildAdded
, you're trying to retrieve a single string value. Since the above JSON object is not a single string value, the getValue(String.class)
returns null
.
To get the values, you can call getValue()
on the individual properties:
lastlastref.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String date_time = dataSnapshot.child("date_time").getValue(String.class);
String note = dataSnapshot.child("note").getValue(String.class);
String vaccine = dataSnapshot.child("vaccine").getValue(String.class);
}
You can also create a minimal class to wrap each record, and read that. The simplest version of that is:
public class ImmunizationRecord {
public String date_time;
public String note;
public String vaccine;
}
And the reading would then be done with:
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String record = dataSnapshot.getValue(ImmunizationRecord.class);
}
A small side note about the way you store date_time
. This format will lead to problems as you progress, since it is not sortable. If you need to store time stamps, store them as milliseconds since the epoch, or in a string format that allows them to be sorted (e.g. 2018-11-16T11:29:00
).
Worked like a charm. Thanks a lot!
– Jackie Chan
Nov 21 at 9:28
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
You set up your reference to:
lastlastref = myRef.child(babyid).child("baby_features").child("immunization_records");
So each child node under this location is a JSON object like this:
{
"date_time": "November/16/2018...",
"note": "hhjj...",
"vaccine": "Measles"
}
But in your onChildAdded
, you're trying to retrieve a single string value. Since the above JSON object is not a single string value, the getValue(String.class)
returns null
.
To get the values, you can call getValue()
on the individual properties:
lastlastref.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String date_time = dataSnapshot.child("date_time").getValue(String.class);
String note = dataSnapshot.child("note").getValue(String.class);
String vaccine = dataSnapshot.child("vaccine").getValue(String.class);
}
You can also create a minimal class to wrap each record, and read that. The simplest version of that is:
public class ImmunizationRecord {
public String date_time;
public String note;
public String vaccine;
}
And the reading would then be done with:
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String record = dataSnapshot.getValue(ImmunizationRecord.class);
}
A small side note about the way you store date_time
. This format will lead to problems as you progress, since it is not sortable. If you need to store time stamps, store them as milliseconds since the epoch, or in a string format that allows them to be sorted (e.g. 2018-11-16T11:29:00
).
Worked like a charm. Thanks a lot!
– Jackie Chan
Nov 21 at 9:28
add a comment |
up vote
0
down vote
accepted
You set up your reference to:
lastlastref = myRef.child(babyid).child("baby_features").child("immunization_records");
So each child node under this location is a JSON object like this:
{
"date_time": "November/16/2018...",
"note": "hhjj...",
"vaccine": "Measles"
}
But in your onChildAdded
, you're trying to retrieve a single string value. Since the above JSON object is not a single string value, the getValue(String.class)
returns null
.
To get the values, you can call getValue()
on the individual properties:
lastlastref.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String date_time = dataSnapshot.child("date_time").getValue(String.class);
String note = dataSnapshot.child("note").getValue(String.class);
String vaccine = dataSnapshot.child("vaccine").getValue(String.class);
}
You can also create a minimal class to wrap each record, and read that. The simplest version of that is:
public class ImmunizationRecord {
public String date_time;
public String note;
public String vaccine;
}
And the reading would then be done with:
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String record = dataSnapshot.getValue(ImmunizationRecord.class);
}
A small side note about the way you store date_time
. This format will lead to problems as you progress, since it is not sortable. If you need to store time stamps, store them as milliseconds since the epoch, or in a string format that allows them to be sorted (e.g. 2018-11-16T11:29:00
).
Worked like a charm. Thanks a lot!
– Jackie Chan
Nov 21 at 9:28
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
You set up your reference to:
lastlastref = myRef.child(babyid).child("baby_features").child("immunization_records");
So each child node under this location is a JSON object like this:
{
"date_time": "November/16/2018...",
"note": "hhjj...",
"vaccine": "Measles"
}
But in your onChildAdded
, you're trying to retrieve a single string value. Since the above JSON object is not a single string value, the getValue(String.class)
returns null
.
To get the values, you can call getValue()
on the individual properties:
lastlastref.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String date_time = dataSnapshot.child("date_time").getValue(String.class);
String note = dataSnapshot.child("note").getValue(String.class);
String vaccine = dataSnapshot.child("vaccine").getValue(String.class);
}
You can also create a minimal class to wrap each record, and read that. The simplest version of that is:
public class ImmunizationRecord {
public String date_time;
public String note;
public String vaccine;
}
And the reading would then be done with:
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String record = dataSnapshot.getValue(ImmunizationRecord.class);
}
A small side note about the way you store date_time
. This format will lead to problems as you progress, since it is not sortable. If you need to store time stamps, store them as milliseconds since the epoch, or in a string format that allows them to be sorted (e.g. 2018-11-16T11:29:00
).
You set up your reference to:
lastlastref = myRef.child(babyid).child("baby_features").child("immunization_records");
So each child node under this location is a JSON object like this:
{
"date_time": "November/16/2018...",
"note": "hhjj...",
"vaccine": "Measles"
}
But in your onChildAdded
, you're trying to retrieve a single string value. Since the above JSON object is not a single string value, the getValue(String.class)
returns null
.
To get the values, you can call getValue()
on the individual properties:
lastlastref.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String date_time = dataSnapshot.child("date_time").getValue(String.class);
String note = dataSnapshot.child("note").getValue(String.class);
String vaccine = dataSnapshot.child("vaccine").getValue(String.class);
}
You can also create a minimal class to wrap each record, and read that. The simplest version of that is:
public class ImmunizationRecord {
public String date_time;
public String note;
public String vaccine;
}
And the reading would then be done with:
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String record = dataSnapshot.getValue(ImmunizationRecord.class);
}
A small side note about the way you store date_time
. This format will lead to problems as you progress, since it is not sortable. If you need to store time stamps, store them as milliseconds since the epoch, or in a string format that allows them to be sorted (e.g. 2018-11-16T11:29:00
).
answered Nov 20 at 17:36
Frank van Puffelen
223k26364390
223k26364390
Worked like a charm. Thanks a lot!
– Jackie Chan
Nov 21 at 9:28
add a comment |
Worked like a charm. Thanks a lot!
– Jackie Chan
Nov 21 at 9:28
Worked like a charm. Thanks a lot!
– Jackie Chan
Nov 21 at 9:28
Worked like a charm. Thanks a lot!
– Jackie Chan
Nov 21 at 9:28
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53386147%2fhow-to-get-child-objects-value-inside-a-key-from-a-list-of-data-using-childeve%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
This means that
babyid
isnull
, which is not allowed in calls tochild()
. You can easily verify the value ofbabyid
by running the code in a debugger, and putting a breakpoint on the line withlastlastref = myRef.child(babyid)...
.– Frank van Puffelen
Nov 20 at 4:34
ok, fixed the null error but it still won't get the children object values... could my code be lacking something?
– Jackie Chan
Nov 20 at 16:03