在addValueEventListener

时间:2019-07-11 09:01:25

标签: java android arrays arraylist

我正在尝试显示RecyclerView中的某些项目,而这些项目包含在填充了ArrayList(Firebase)数据的RealtimeDatabase中。每次在数据库中检测到某些更改时都会执行此操作,如下所示。问题是lstCompany之后addValueEventListener变为空。我怎样才能使它满足?

public class FragmentCompanies extends Fragment {
    View v;
    private RecyclerView recyclerView;
    private List<Company> lstCompany;
    DatabaseReference db;

    public FragmentCompanies(){}

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        db = FirebaseDatabase.getInstance().getReference();
        db.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                lstCompany = new ArrayList<>();
                DataSnapshot ds = dataSnapshot.child("Company");
                for (DataSnapshot company: ds.getChildren()){
                    Map<String, String> currCompany = (Map) company.getValue();
                    Company c = new Company(currCompany.get("name"), currCompany.get("overview"), currCompany.get("imageURL"));
                    lstCompany.add(c);
                    System.out.println(lstCompany);
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
        System.out.println(lstCompany);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState){
        v = inflater.inflate(R.layout.fragment_companies,container,false);

        System.out.println(lstCompany);

        //bind the adapter to the recyclerView
        recyclerView = v.findViewById(R.id.companies_recyclerview);
        CompaniesRecyAdapter recyAdapter = new CompaniesRecyAdapter(getContext(), lstCompany);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.setAdapter(recyAdapter);

        return v;
    }
}

最后两个println操作返回null,并且不应发生。

1 个答案:

答案 0 :(得分:1)

在onCreate上初始化数组列表,并在onDataChange上添加数据并通知适配器数据已更改

public class FragmentCompanies extends Fragment {
    View v;
    private RecyclerView recyclerView;
    private List<Company> lstCompany;
    DatabaseReference db;
    public FragmentCompanies(){}

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState){
        v = inflater.inflate(R.layout.fragment_companies,container,false);

        lstCompany = new ArrayList<>();
        System.out.println(lstCompany);

        //bind the adapter to the recyclerView
        recyclerView = v.findViewById(R.id.companies_recyclerview);
        CompaniesRecyAdapter recyAdapter = new CompaniesRecyAdapter(getContext(), lstCompany);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.setAdapter(recyAdapter);

        db = FirebaseDatabase.getInstance().getReference();
        db.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                DataSnapshot ds = dataSnapshot.child("Company");
                for (DataSnapshot company: ds.getChildren()){
                    Map<String, String> currCompany = (Map) company.getValue();
                    Company c = new Company(currCompany.get("name"), currCompany.get("overview"), currCompany.get("imageURL"));
                    lstCompany.add(c);
                    System.out.println(lstCompany);
                    System.out.println(c.getCompanyName);
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
        System.out.println(lstCompany);





        return v;
    }
}