问题
I'm currently trying to read the contents from my Firebase Database into a RecyclerView but I want to load them from the last node to first node. So I decided to place the keys into an array then try to read from end of the array to the front, but when I try to access the key array I am presented with Can't pass null for argument 'pathString' in child() error.
Below is the method I am using to load the data:
@Override
public void onStart() {
super.onStart();
FirebaseRecyclerAdapter<UpcomingList, UpcomingViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<UpcomingList, UpcomingViewHolder>(
UpcomingList.class,
R.layout.upcoming_event_row,
UpcomingViewHolder.class,
mDatabase
) {
@Override
protected void populateViewHolder(final UpcomingViewHolder viewHolder, UpcomingList model, int position) {
/*String CurrentString = date.toString().trim();
StringTokenizer tokens = new StringTokenizer(CurrentString, "/");
final String first = tokens.nextToken();
final String second = tokens.nextToken();*/
//final String post_key = getRef(position).getKey();
mDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
int i = 0;
for (DataSnapshot d : dataSnapshot.getChildren()) {
key[i] = d.getKey();
i++;
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
for (int counter = key.length - 1; counter >= 0; counter--) {
viewHolder.setUpDate(first + "/" + key[counter]);
mDatabase.child(key[counter]).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
StringBuilder builder = new StringBuilder();
for (DataSnapshot child : dataSnapshot.getChildren()) {
builder.append(child.getKey().toString() + "\n\n");
}
viewHolder.setName(builder.toString());
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
};
upcomingRV.setAdapter(firebaseRecyclerAdapter);
}
Your assistance is appreciated.
回答1:
You're being bitten by asynchronous programming 101. You need to make sure that the code that requires the key[] contents is inside the onDataChange that is called once the keys are loaded.
In this case that means it should look something like thisL
mDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
int i = 0;
for (DataSnapshot d : dataSnapshot.getChildren()) {
key[i] = d.getKey();
i++;
}
for (int counter = key.length - 1; counter >= 0; counter--) {
viewHolder.setUpDate(first + "/" + key[counter]);
mDatabase.child(key[counter]).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
StringBuilder builder = new StringBuilder();
for (DataSnapshot child : dataSnapshot.getChildren()) {
builder.append(child.getKey().toString() + "\n\n");
}
viewHolder.setName(builder.toString());
}
@Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException(); // don't ignore errors
}
});
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException(); // don't ignore errors
}
});
来源:https://stackoverflow.com/questions/49371687/cant-pass-null-for-argument-pathstring-in-child