当前位置: 首页 > 知识库问答 >
问题:

不能为child()中的参数“path string”传递null

王佐
2023-03-14

我目前正在尝试将我的Firebase数据库中的内容读入recyclerView中,但我希望将它们从最后一个节点加载到第一个节点。所以我决定将键放入一个数组中,然后尝试从数组的末尾读到前面,但是当我尝试访问键数组时,我看到的是不能为child()中的参数'path string'传递null错误。

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

共有1个答案

瞿健
2023-03-14

你正在被异步编程101所折磨。您需要确保需要键[]内容的代码在OnDataChange中,一旦加载键,就会调用该代码。

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

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
    }
});
 类似资料: