ChandraKumar
ChandraKumar

Reputation: 547

How to update listview in android dynamically using firebase database?

    reference.child("category").addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            Log.d("Firebase",dataSnapshot.getValue(String.class)+" Key is "+Integer.parseInt(dataSnapshot.getKey()));
            cat.add(Integer.parseInt(dataSnapshot.getKey()),dataSnapshot.getValue(String.class));
            if(Integer.parseInt(dataSnapshot.getKey())==0) {
                adapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.support_simple_spinner_dropdown_item, cat);
                catList.setAdapter(adapter);
            }
            else {
                adapter.notifyDataSetChanged();
            }
        }

I want to show the category in the list view. But when I executed this code only the first category is shown but in the Log both the category are shown(There are two category).

Upvotes: 0

Views: 223

Answers (1)

Alex Mamo
Alex Mamo

Reputation: 138969

Take the following code out from the loop:

if(Integer.parseInt(dataSnapshot.getKey())==0) {
    adapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.support_simple_spinner_dropdown_item, cat);
    catList.setAdapter(adapter);
}

Hope it helps.

Upvotes: 2

Related Questions