Getting the names of items from firebase Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Getting the names of items from firebase without wasting too much if your time.

The question is published on by Tutorial Guruji team.

FirebaseStorage storage;
StorageReference downloadRef;
StorageReference listRef;
Object Downloads;
ArrayList<String> arrayList;
ArrayAdapter arrayAdapter;
ListView listView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    listView = findViewById(R.id.listView);
    arrayList = new ArrayList<>();

    listRef = storage.getInstance().getReference("Project");
    listRef.listAll().addOnSuccessListener(new OnSuccessListener<ListResult>() {
        @Override
        public void onSuccess(ListResult listResult) {
            for (StorageReference items : listResult.getItems()) {
                arrayList.add(items.getName());
            }
        }
    });

    arrayAdapter = new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_1, arrayList);
    listView.setAdapter(arrayAdapter);

}

So I am new to android, and this is what I tried to get the names of the documents so I can display them with listView. What am I getting wrong?

Answer

It seems like your onSuccess is getting called, which means you get a list of files from Firebase.

What’s most likely happening is that you’re processing this list correctly, but then not telling the adapter that you’ve changed its data. To tell the adapter that its data has changed call notifyDataSetChanged on it:

listRef.listAll().addOnSuccessListener(new OnSuccessListener<ListResult>() {
    @Override
    public void onSuccess(ListResult listResult) {
        for (StorageReference items : listResult.getItems()) {
            arrayList.add(items.getName());
        }
        arrayAdapter.notifyDataSetChanged();
    }
});

At that point the adapter will repaint the associated views, and your data should show up.

We are here to answer your question about Getting the names of items from firebase - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji