Frank
Frank

Reputation: 69

ArrayList becomes null after addValueEventListener

I am trying to show some items in a RecyclerView and such items are contained in an ArrayList populated with RealtimeDatabase (Firebase) data. This is done every time some changes are detected in the db as follows. The problem is that lstCompany becomes null soon after the addValueEventListener. How can I keep it fulfilled?

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

The last two println operations return null and it should not happen.

Upvotes: 0

Views: 129

Answers (1)

Abdul
Abdul

Reputation: 887

intialize the arraylist in onCreate, and when add data in on onDataChange and notify the adapter that data has been changed

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

Upvotes: 1

Related Questions