What I want to get is "Kasarwadi" and similar node that will be added after. I have to populated spinner with this data. How can I loop over to get this?
Also when I tried to loop over in the event Listeners, foreach loop is not getting executed or I can say control is not getting transferred in foreach (enhanced for loop)loop.
code for ref`
DatabaseReference mDatabaseReference = FirebaseDatabase.getInstance().getReference("people-3999c");
mDatabaseReference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
Log.d("STATE"," inside ondataChange");
for (DataSnapshot location : dataSnapshot.getChildren()){
Log.d("STATE","1");
locationFromFB.add(location.getKey());
ArrayAdapter<String> adapter = new ArrayAdapter<>(getActivity(),android.R.layout.simple_spinner_item,locationFromFB);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mSpinner.setAdapter(adapter);
}
Log.d("STATE","outside");
}`
In order to use that locationFromFB ArrayList you need to declare it inside onDataChange() method, otherwise is null, due the asynchronous behaviour.
Get also the following line ouf of the for loop because you are creating a new instance every time the for loop iterates.
ArrayAdapter<String> adapter = new ArrayAdapter<>(getActivity(),android.R.layout.simple_spinner_item,locationFromFB);
Please change also the DatabaseReference like this:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference yourRef = rootRef.child("Kasarwadi").child("Users");
Hope it helps.
Related
I am trying to pull the data from my firebase real time database. The problem i am facing here is that I can pull the data from /ItemList successfully but when I try to pull the data from /ItemRequest the data cant be pulled but when I give it a specific path such as /ItemRequest/admin it works. What I am trying to do is to populate a listview with all the child from /ItemRequest from both admin and user.I am using firebase list adapter to populate my listview so are there any ways to do what i want?
Database Layout
What I am trying to do is to populate a listview with all the child from /ItemRequest from both admin and user.
To solve this, you need to iterate over the DataSnapshot object twice, like in the following lines of code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference itemRequestRef = rootRef.child("ItemRequest");
ValueEventListener valueEventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot dSnapshot : dataSnapshot.getChildren()) {
for(DataSnapshot ds : dSnapshot.getChildren()) {
String productName = ds.child("ProductName").getValue(String.class);
Log.d(TAG, productName);
}
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
}
};
itemRequestRef.addListenerForSingleValueEvent(valueEventListener);
The output in your logcat will be:
Manix
Manix
Manix
If you want to display those results in a ListView, please see my answer from this post:
Showing Firebase data in ListView
AFAIK, FirebaseListAdapter does not work well with child objects that have multiple childs themselves and there is no query method for you to filter them out. I would rather manually pull the child items one by one using DataSnapshots adding them into an arraylist then populating the list using a custom array adapter instead
I want to sort my items in the listview alphabetically, I tried orderByChild("name") but I don't think it even worked. However here's my code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference medicinesRef = rootRef.child("Medicines");
ValueEventListener eventListener = new ValueEventListener() {
#Override
public void onDataChange(com.google.firebase.database.DataSnapshot dataSnapshot) {
for(com.google.firebase.database.DataSnapshot ds : dataSnapshot.getChildren()) {
medName = ds.child("name").getValue(String.class);
Log.d("TAG", medName);
myArrayList.add(medName);
ArrayAdapter<String> firebaseAdapter = new ArrayAdapter<String>(MedicineSearch.this, android.R.layout.simple_list_item_1, myArrayList);
lv.setAdapter(firebaseAdapter);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
}; // populates listview
medicinesRef.addListenerForSingleValueEvent(eventListener);
rootRef.keepSynced(true);
In order to sort data, you need to use a Query and for that, please see the following code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference medicinesRef = rootRef.child("Medicines");
Query query = medicinesRef.orderByChild("name");
query.addListenerForSingleValueEvent(/* ... */);
Another approach would be use the same code and to sort the list using:
Collections.sort(myArrayList);
But do not create and use the adapter inside the for loop. Move those two lines of code outside the loop, otherwise you end up having a single item in your ListView.
I'm starting to code Android Apps with the database connection. As IDE I use Android Studio and Firebase for the database. This is my data structure in Firebase:
In my app, I need to fill a ListView with all the names. So I want the data from three children from the child "Rezepte" from the UUID child. I hope this is understandable.
I've done a lot of googling and I just can't figure out how this works. My current version of the java code is this:
final FirebaseUser user = firebaseAuth.getCurrentUser();
// trying to get the reference of the right path
DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child(user.getUid()).child("Rezepte");
ref.addListenerForSingleValueEvent(
new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
// calling a method which should read the data from the snapshot and put it into the ListView
fillList((Map<String, Object>) dataSnapshot.child(user.getUid()).child("Rezepte").getValue());
}
#Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(RezepteActivity.this, "Daten konnten nicht gelesen werden", Toast.LENGTH_SHORT).show();
}
});
}
private void fillList(Map<String, Object> entries) {
// loop to cycle through every child of "Rezepte" and get the names
for (Map.Entry<String, Object> entry : entries.entrySet()) {
Map singleEntry = (Map) entry.getValue();
// add the name to an ArrayList of all names
Rezepte.add((String) singleEntry.get("name"));
}
// put the data into the ListView
lvRezepte.addFooterView(lvRezepte, Rezepte, true);
}
The app always crashes and I don't really understand a lot in the debugger. But the error occurs already in the onDataChange void.
I would be really happy if someone could help me.
You're trying to traverse down the hierarchy in your result DataSnapshot when you've already done so in your query itself. So in your onDataChange() you need to just put:
fillList(datasnapshot.getValue()) //Map of results at /{uid}/Rezepte
Assuming that fillList method works properly, you should first fix the following line
fillList((Map<String, Object>) dataSnapshot.child(user.getUid()).child("Rezepte").getValue())
to
fillList((Map<String, Object>) dataSnapshot.getValue())
You already have a reference to Rezepte child of the current user when you declared it like this:
DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child(user.getUid()).child("Rezepte")
Your snapshot has 1, 2, and 3 as children, so you shouldn't reinforce the path you declared in the reference.
To get all those names and add them to an ArrayList, please use the following code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference rezepeteRef = rootRef.child(user.getUid()).child("Rezepte");
ValueEventListener eventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
List<String> list = new ArrayList<>();
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String name = ds.child("name").getValue(String.class);
list.add(name);
Log.d("TAG", name);
}
Log.d("TAG", list);
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
rezepeteRef.addListenerForSingleValueEvent(eventListener);
Hi I'm new in firebase and android studio and I have a problem
I want to store the data of this database in an array and next use the array to make a recycler view
Then in the main activity I wrote this:
And if I run the app only this is shown:
But if I do the same but instead of firebase i use a String[] with the data i want to show it works but i don't know why with firebase i can't.
if I change the references by this:
reference = database.getReference(FirebaseReferences.POKEMONS_REFERENCES);
reference.child(FirebaseReferences.APP_REFERENCES).addValueEventListener(new ValueEventListener() {
when I run the app, it automatically closes and i don't know what to do.
Could you please tell me what i'm doing wrong?
Thank you in advance.
Here is the logCat
https://github.com/adcasna/SearchViewPrueba2/wiki
Fisrts of all, please note that the Firebase documentation recommends against using arrays. Using an array is an anti-pattern when it comes to Firebase. One of the many reasons Firebase recommends against using arrays is that it makes the security rules impossible to write. If you still want to use this kind of structure, for getting those names and store them in an ArrayList, please use the following code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference appRef = rootRef.child("pokemons").child("app");
ValueEventListener eventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
List<String> list = new ArrayList<>();
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String c_name = ds.child("c_name").getValue(String.class);
list.add(c_name);
Log.d("TAG", c_name);
}
Log.d("TAG", list );
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
appRef.addListenerForSingleValueEvent(eventListener);
the situation is following, I'm using Firebase DB for a small application, the structure of DB is the following.
I'm aware this kind of structuring is not the best for Firebase, probably the problem is here.
My question is, when I'm trying to get childrens list from a parent, Firebase provides me with childrens list from one level deeper entity. For ex. related to this DB sample - if I try to get childrens list from the "University" entity, I get the output "Faculty", but should get the output "UNN". Am I right?
Here is the code sample I'm using for retreiving data:
mDBRef = FirebaseDatabase.getInstance().getReference().child("University");
final ListView listView = (ListView) findViewById(R.id.lv_main);
mGruppeChildEventListener = new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
List<String> lst = new ArrayList<String>(); // Result will be holded Here
for (DataSnapshot dsp : dataSnapshot.getChildren()) {
lst.add(String.valueOf(dsp.getKey())); //add result into array list
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, lst);
listView.setAdapter(adapter);
}
Well it is because of firebase's onChildAdded method come with parameter of children of the parent you are reading from. If you added child event listener on 'University', you will get value like 'UNN'-->Faculty. Again if you perform getChildren method on this, you will get 'Faculty'-->RGF. So here if you apply getKey, off course it will return Faculty.