sourav kadam
sourav kadam

Reputation: 43

List View is not showing my items from FIREBASE

Contents of collection 'list_of_groups' are updated, but never queried , this is the only issue i could spot in my code.

   private View groupFragmentView;
    private ListView list_View;
    private ArrayAdapter<String> arrayAdapter;
    private ArrayList<String> list_of_groups = new ArrayList<>();
    private DatabaseReference GroupRef;

there is no issue in this part of code ...

 ` 
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        // Inflate the layout for this fragment
        groupFragmentView =  inflater.inflate(R.layout.fragment_groups, container, false);

        GroupRef = FirebaseDatabase.getInstance().getReference().child("Groups");

        InitializeFields(groupFragmentView);

        RetriveAndDisplayGroups();


        return groupFragmentView;
    }

    private void RetriveAndDisplayGroups()
    {
        GroupRef.addValueEventListener(new ValueEventListener()
        {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot)
            {
                Set<String> set = new HashSet<>();
                Iterator iterator = snapshot.getChildren().iterator();

                while (iterator.hasNext())
                {
                    set.add(((DataSnapshot)iterator.next()).getKey());
                }

                list_of_groups.clear();
                list_of_groups.addAll(set);
                arrayAdapter.notifyDataSetChanged();
            }

          
    }

    private void InitializeFields(View view)
    {
        list_View = (ListView) view.findViewById(R.id.list_View);
        arrayAdapter = new ArrayAdapter<String>(getContext(),android.R.layout.simple_list_item_1);
        list_View.setAdapter(arrayAdapter);

    }`

This is my code it is not showing my groups names which are stored in my firebase REALTIME DATABASE, there is no error in code, and data is being stored in database as well.

I tried cleaning and building my project it did not work.

here is my xml file.

`

<ListView
        android:id="@+id/list_View"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />`

Upvotes: 1

Views: 73

Answers (1)

David Kroukamp
David Kroukamp

Reputation: 36423

In your onDataChange method you never update your ArrayAdpater with the values from list_groups. You should call addAll on your ArrayAdpater before calling notifyDataSetChanged()

Something like:

arrayAdapter.addAll(list_groups);
arrayAdapter.notifyDataSetChanged()

Upvotes: 1

Related Questions