I'm doing my project.
My project is App for finding room in my campus.
In my database there are room name, floor, building room and location.
I want to find all records that have a keyword that the user want to find.
And show data about that room.
My firebase
Refer this link for complete code
http://www.amalhichri.net/android-firebase-realtime-database-basic-cruds/
private void findperson(String name){
Query deleteQuery = myDatabaseReference.orderByChild("fullName").equalTo(name);
deleteQuery.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Iterable snapshotIterator = dataSnapshot.getChildren();
Iterator iterator = snapshotIterator.iterator();
while((iterator.hasNext())){
Log.d("Item found: ",iterator.next().getValue().toString()+"---");
}
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}`enter code here`
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
Log.d("Item not found: ","this item is not in the list");
}
});
}
Change your firebase url to
"YourFireBaseUrl/room/cb1201" to get the data of room no cb1201
you will get the data in JSON format
Related
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()"
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).
I want to get particular value using from DataSnapshot.
I am attaching the screenshot here so kindly check and help me to get particular value from Realtime Database.
Actually I am implementing chat application in which I want to get value of user from group_list.
Here is my code.
private void loadTotalGroupList() {
referenceMainUrl = FirebaseDatabase.getInstance().getReferenceFromUrl("https://pure-coda-174710.firebaseio.com");
referenceGroupList = referenceMainUrl.child("group_list");
//Check if child is available or not.
referenceGroupList.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
Log.e("dataSnapshot"," ==>"+dataSnapshot);
Map<String, Object> newPost = (Map<String, Object>) dataSnapshot.getValue();
Log.e("newPost"," ==>"+newPost);
Log.e("user: ","==>" + newPost.get("user")); // Here I am getting null value
} else {
Log.e("Child not found", " >>>");
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
and log showing like this. DataSnapshot { key = group_list, value = {First Group={-KtBH9gnTszNxcXjNu9A={message=assaasas, user=sakib}}} }
I have resolved my issue by using addChildEventListener
referenceGroupList.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String prevChildKey) {
Log.e("dataSnapshot KEY", " ==>" + dataSnapshot.getKey());
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String prevChildKey) {
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String prevChildKey) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Here dataSnapshot.getKey() returns all sub child of it.
You're listening to the location /group_list in your code. That means a a snapshot from that location will contain the following:
/First Group
/-KtBH9gnTszNxcXjNu9A
message = "..."
user = "..."
If you want to get the user value from that location, you'll have to dig into it using each intermediate path:
dataSnapshot.child("First Group").child("-KtBH9gnTszNxcXjNu9A").child("user").getValue()
Or more simply:
dataSnapshot.child("First Group"/-KtBH9gnTszNxcXjNu9A/user").getValue()
You can't skip the middle paths in the snapshot. Alternatively, you may want to listen to a location closer to the value you want:
referenceMainUrl.child("group_list/First Group/-KtBH9gnTszNxcXjNu9A");
This code runs in a loop and gives you values for all the 'user's
DatabaseReference dRef = FirebaseDatabase.getInstance().getReference().child("group_list").child("First_Group");
dRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot snapshot : dataSnapshot.getChildren()){
String userName = snapshot.child("user").getValue(String.class);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
HI everyone I want to catch all the keys on my firebase database:
Users-
26dfg678-
Name: jack
Job: Farmer
43jkhjh4-
Name: bill
Job: ICT
I want to catch the id: 26dfg678 and 43jkhjh4 and put them in an array. This is my code:
final DatabaseReference database_nomi = firebaseDatabase.getReference().child("Users");
database_nomi.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
//here
**name[0] = dataSnapshot.getKey();
Provee.setText(name[0]);**
}
#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) {
}
});
if i do this it takes only the last but i want all..
To do this you need to read through your Firebase snapshot using a ValueEventListener (and addListenerForSingleValueEvent).
databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
int i = 0;
for(DataSnapshot d : dataSnapshot.getChildren()) {
name[i] = d.getKey();
i++;
}
}
}//onDataChange
#Override
public void onCancelled(DatabaseError error) {
}//onCancelled
});
This code uses a foreach to read all dataSnapshot children, saving its key in an array at each iteration.
I am having a hard time with figuring out how to query my Firebase database. Here is what it looks like.
And here is my code:
//RETRIEVE
public ArrayList<Spacecraft> retrieve()
{
String myUserId = acct.getId();
//db.addChildEventListener(new ChildEventListener() {
db.child("/users/uid").equals(myUserId)
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
//fetchData(dataSnapshot);
fetchData(dataSnapshot);
adapter.notifyDataSetChanged();
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
fetchData(dataSnapshot);
adapter.notifyDataSetChanged();
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
return spacecrafts;
}
So db.addChildEventListener will retrieve the entire database. But what I want is to only retrieve data for users whose uid is equal to String myUserId. And I want to sort in ascending order by Level. I have read the docs and watched videos but I cannot figure it out. Any help would be appreciated.
Query query = db.child("users").orderByChild("uid").equalTo("myUserId");
query.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot snapshot) {
for (DataSnapshot userSnapshot: snapshot.getChildren()) {
System.out.println("User "+userSnapshot.child("uid").getValue());
}
}
...
But if you're frequently accessing the data by UID, you're off restructuring your database to store all users under their own UID:
users
myUserId
Level: 2
NumCorrect: 8
You can then read the data with:
db.child("users/myUserId").addValueEventListener(new ValueEventListener() {
public void onDataChange(DataSnapshot snapshot) {
fetchData(dataSnapshot);
}
For more on Firebase queries, see the Firebase documentation on sorting and filtering data. Since you're new to NoSQL, I also recommend reading NoSQL data modeling and viewing Firebase for SQL developers.
You can reverse the array list
ArrayList<UserModel> user_list = new ArrayList<>();
for (DataSnapshot snapshot :dataSnapshot.getChildren()) {
UserModel userModel = snapshot.getValue(UserModel.class);
user_list.add(userModel);
Collections.reverse(user_list);
}
I wanna import from Firebase list and sublist. Firebase Database looks something like this:
List_1
sublist_1 name
sublist_2 data
sublist_3 age
sublits 4 Location
List_2
sublist_1 name
sublist_2 data
sublist_3 age
sublits 4 Location
List_3
sublist_1 name
sublist_2 data
sublist_3 age
sublits 4 Location
This is what I have for now in Android Studio:
#Override
protected void onStart() {
super.onStart();
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mCursa);
mListViewCurse.setAdapter(adapter);
Firebase curseRef = mRootRef.child("List_1");
curseRef.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String message = dataSnapshot.getValue(String.class);
Log.v("E_CHILD_ADDED", message);
mCursa.add(message);
adapter.notifyDataSetChanged();
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
String message = dataSnapshot.getValue(String.class);
Log.v("E_CHILD_CHANGED", message);
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
String message = dataSnapshot.getValue(String.class);
Log.v("E_CHILD_REMOVED", message);
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onCancelled(FirebaseError firebaseError) {
}
});
}
This is the output:
name
data
age
location
Could someone please tell me what I need to add so I can see all the lists in my Android app?