问题
Above is just an example of the structure of the folder in FirebaseDatabase. My query is that how am I suppose to get those child node value.
Below is my code which I tried:
//FirebaseDatabase Collecting all the Item List
ValueEventListener listener= new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()){
for (DataSnapshot ds: dataSnapshot.getChildren()){
arrayItemList.add(ds.getKey());
}
firebaseRecyclerViewItem(arrayItemList);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.i(TAG, "onCancelled: Unable to fetch data");
}
};
lookUpDatabase.addValueEventListener(listener);
Here is code for the Method firebaseRecyclerViewItem():
public void firebaseRecyclerViewItem(final ArrayList<String> arrayItemList) {
DatabaseReference reference = null;
String referenceName = null;
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
//FirebaseDatabase 2'nd Reference Address
for (DataSnapshot ds : dataSnapshot.getChildren()) {
String key1 = ds.getKey();
Log.i(TAG, "onDataChange: " + key1);
String firebaseItemName = ds.child("DATA").getValue(String.class);
Log.i(TAG, "onDataChange: " + firebaseItemName);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
};
for (int i = 0; i < arrayItemList.size(); i++) {
referenceName = arrayItemList.get(i);
reference = FirebaseDatabase.getInstance().getReference().child("DATA").child(referenceName);
reference.addValueEventListener(valueEventListener);
//Loading RecyclerView
RecyclerViewItemList(reference, referenceName);
}
}
Is not that I am not able to get the values, I am able to get it however in the database pic which I have attached there are three FILE01, FILE02 and FILE03. I am only able to get the last FILE03, "01" value, I mean ValueEventListner skips all the other FILE present there only take the Last update or the last file. But I want all the files data present there to display it on my recyclerView.
I have also tried a different approached where I get the DataSnaphot, and use the for() Loop, where I used nested loop:
//FirebaseDatabase Collecting all the Item List
ValueEventListener listener= new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()){
// here is where the changes which I have made, I tried to called the value which is impossible but still I tried to, but not result.
for (DataSnapshot ds: dataSnapshot.getChildren()){
String key= ds.getKey();
String firebaseItemName = ds.child("DATA").getValue(String.class);
Log.i(TAG, "onDataChange: " + firebaseItemName);
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.i(TAG, "onCancelled: Unable to fetch data");
}
};
lookUpDatabase.addValueEventListener(listener);
RecyclerViewItemList(lookUpDatabase);
Is there a way or a method which I can use to call the values.
回答1:
You should call notifyDataSetChanged() in for loop. Otherwise your reference and referenceName will be rewrited.
So your code should be
for (int i = 0; i < arrayItemList.size(); i++){
//Add your data to arraylist
rvAdapter.notifyDataSetChanged();
}
Edit:
This will show your firebase data. I just changed your second method.
/*I changed Here*/
private ArrayList<HashMap<String,String>> yourChildList = new ArrayList<>();
ValueEventListener listener= new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()){
for (DataSnapshot ds: dataSnapshot.getChildren()){
/*I changed Here*/
HashMap<String,String> dataMap = new HashMap<>();
String key= ds.getKey();
String firebaseItemName = ds.child("DATA").getValue(String.class);
Log.i(TAG, "onDataChange: " + firebaseItemName);
/*I changed Here*/
dataMap.put(key,firebaseItemName);
yourChildList.add(yourDataMap);
}
/*I changed Here
*
*I think this method is used to update data to your recyclerView
*if I'm right, you can replace it
*RecyclerViewItemList(lookUpDatabase);
*
*/
yourRecyclerAdapter = new RecyclerAdapter(yourChildList);
yourRecyclerAdapter.notifyDataSetChanged();
recyclerView.setAdapter(yourRecyclerAdapter);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.i(TAG, "onCancelled: Unable to fetch data");
}
};
lookUpDatabase.addValueEventListener(listener);
Note:I don't recommend you to use nested listeners. You should avoid nested listener for better performance and maintainability. You should use Observer or LiveData to update Recyclerview.
来源:https://stackoverflow.com/questions/62099116/unable-to-call-values-from-firebasedatabase-realtime-using-firebaseui