简体   繁体   中英

How to populate listview with images from Firebase Database in android?

I have stored links to the images in my Firebase storage to my Firebase Database. The JSON structure for my database is something like this:

|
 --- users
 |
 |
    ---- uid1
 |  |
 |   -- uid1
 |  |
 |   -- name
 |  |
 |   -- imageURL
 |  |
 |
 |
  -- uid2

What I want to do is populate my listview with images from this. I also need to know how to populate listview with images.

I read some answers on stack overflow but I couldn't understand how should I start with this.

I would appreciate if someone could provide information and some code to start with this.

EDIT : I am able to get the imageURL from the database, I was not stuck in that. I want to know how can I populate listview with images? I saw some answers on StackOverflow but none of them could properly address my problem.

This can be done by fetching the image url from Firebase in your adapter or wherever you want to set your image. A sample code assuming you have a list of images to use:

FirebaseDatabase.getInstance().getReference().child("users").addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    for(DataSnapshot ds:dataSnapshot.getChildren()){
                        String image = ds.child("image").getValue(String.class);
                        // You can now set your Image with any method you want, Glide Picasso or any other library
//                        GlideApp.with(getContext())
//                                .load(image)
//                                .placeholder(new ColorDrawable(getResources().getColor(R.color.colorWhite)))// you can use any color here
//                                .fitCenter()
//                                .into(image_view);// An ImageView instance
                    }
                }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {

                }
            });

What is being done here, should be where you fetch your items to be displayed in the ListView. After obtaining the list, you can then pass them into your adapter. The commented GlideApp call is supposed to happen in the getView method of your adapter

If you can use the retrofit to fetch the images from Firebase, then it would be very easy to populate the images into RecyclerView. If you are not aware of the retrofit, you can follow this great tutorial. https://www.raywenderlich.com/4539-android-networking

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM