使用addListenerForSingleValueEvent侦听器方法后,数组列表将为空

时间:2019-06-25 05:50:27

标签: java android firebase firebase-realtime-database

我正在从Firebase数据库中获取数据到recyclerview。我无法将数据获取到RecyclerViewAdapter,因为在addListenerForSingleValueEvent方法之后,数组列表变为空。

@Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        final View view = inflater.inflate(R.layout.fragment_dashboard, container, false);
        database = FirebaseDatabase.getInstance();
        ArrayList<Item> arr = new ArrayList<Item>();


        DatabaseReference reference = database.getReference("NoticeBoard");

        reference.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                for (DataSnapshot singleSnapshot : dataSnapshot.getChildren()) {

                    String text1 = (String) singleSnapshot.child("Text1").getValue();
                    String text2 = (String) singleSnapshot.child("Text2").getValue();

                    arr.add(new Item(text1, text2));
                }
            }
            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
            }
        });

        RecyclerViewAdapter adapter = new RecyclerViewAdapter(getContext(), arr);// Here the ArrayList arr is empty

        RecyclerView recyclerView = view.findViewById(R.id.dashboardRecyclerView);

        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.setAdapter(adapter);

        return view;
    }

2 个答案:

答案 0 :(得分:1)

onDataChange()是异步的,因此,由于您在addListenerForSingleValueEvent之后调用arraylist,因此它将返回一个空列表,因为尚未检索到数据。

要解决您的问题,您需要在onDatachange()内添加arrayList:

        reference.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                for (DataSnapshot singleSnapshot : dataSnapshot.getChildren()) {

                    String text1 = (String) singleSnapshot.child("Text1").getValue();
                    String text2 = (String) singleSnapshot.child("Text2").getValue();
                    ArrayList<Item> arr = new ArrayList<Item>();
                    arr.add(new Item(text1, text2));

                    RecyclerViewAdapter adapter = new RecyclerViewAdapter(getContext(), arr);
                    RecyclerView recyclerView = view.findViewById(R.id.dashboardRecyclerView);

                    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
                    recyclerView.setAdapter(adapter);
                }
            }
            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
            }
        });

答案 1 :(得分:1)

Firebase数据库调用不是异步的,这意味着在您从firebase获得响应之前,将执行以下查询行。

因此,如下更新代码以按预期工作。

 RecyclerViewAdapter adapter;

 @Override
 public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    final View view = inflater.inflate(R.layout.fragment_dashboard, container, false);
    database = FirebaseDatabase.getInstance();
    ArrayList<Item> arr = new ArrayList<Item>();

    adapter = new RecyclerViewAdapter(getContext(), arr);// Here the ArrayList arr is empty

    RecyclerView recyclerView = view.findViewById(R.id.dashboardRecyclerView);

    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    recyclerView.setAdapter(adapter);

    DatabaseReference reference = database.getReference("NoticeBoard");

    reference.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

            for (DataSnapshot singleSnapshot : dataSnapshot.getChildren()) {

                String text1 = (String) singleSnapshot.child("Text1").getValue();
                String text2 = (String) singleSnapshot.child("Text2").getValue();

                arr.add(new Item(text1, text2));
            }
            if(adapter != null){
               adapter.notifyDataSetChanged();
            }
        }
        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
        }
    });

    return view;
}

注意事项最好在片段中创建视图后设置数据。您可以为此使用onViewCreated()分段方法。