无法为child()中的参数"pathString"传递null [英] Can't pass null for argument 'pathString' in child()
问题描述
我当前正在尝试将Firebase数据库中的内容读取到RecyclerView
中,但是我想将它们从最后一个节点加载到第一个节点.因此,我决定将键放入数组中,然后尝试从数组末尾读取到前端,但是当我尝试访问键数组时,出现Can't pass null for argument 'pathString' in child()
错误.
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);
}
感谢您的帮助.
推荐答案
您被异步编程101所困扰.您需要确保需要key[]
内容的代码在内部 加载密钥后调用的onDataChange
.
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.
在这种情况下,这意味着它应该看起来像这样L
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
}
});
这篇关于无法为child()中的参数"pathString"传递null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!