gpt4 book ai didi

java - 无法在 child() 中为参数 'pathString' 传递 null

转载 作者:行者123 更新时间:2023-12-02 11:29:39 24 4
gpt4 key购买 nike

我目前正在尝试将 Firebase 数据库中的内容读取到 RecyclerView 中,但我想将它们从最后一个节点加载到第一个节点。因此,我决定将键放入数组中,然后尝试从数组末尾读取到前面,但是当我尝试访问键数组时,我会看到 Can't pass null for argument 'pathString' in child() 错误。

下面是我用来加载数据的方法:

@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 内加载 key 后将调用该函数。

在这种情况下,这意味着它应该看起来像这样L

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
}
});

关于java - 无法在 child() 中为参数 'pathString' 传递 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49371687/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com