简体   繁体   中英

Retrieve Firebase images as drawables and display in recycler View

i have implemented RecyclerView that displays images succesfully and everthing is working fine now in my Frgament i want to load the images from Firebase (images already stored on Realtime Database with their urls) this is my Fragment Code

public RecyclerView youthRecycler;
public RecyclerView.Adapter recyclerAdapter;
public RecyclerView.LayoutManager recyclerManager;


//i just want to populate this array with drawables from images stored on firebase
int[] imagesArray={R.drawable.youthtable1,R.drawable.youthtable2,R.drawable.youthtable3};

public tableYouthFragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_table_youth, container, false);
}

@Override
public void onStart() {
    super.onStart();
    //all code goes here
    init();
}
public void init()
{
    youthRecycler=getActivity().findViewById(R.id.youthRecycler);
    youthRecycler.setHasFixedSize(true);
    recyclerManager=new LinearLayoutManager(getContext());
    youthRecycler.setLayoutManager(recyclerManager);
    recyclerAdapter =new MyRecyclerAdpater(getActivity(),imagesArray);
    youthRecycler.setAdapter(recyclerAdapter);
}

this question is already asked here but without any verified answers, and the verified answers where in kotlin and the java answers required that i change my RecyclerView whole Implementation. i followed up many tutorials on glide library also but didn't know if it's even possible to load the images as drawables or i must change my whole adapter implementation

Your recyclerView should take a list of url's and u can load them in Glide why u need to load them as drawable and glide can load them as url:

in onBindViewHolder():

Glide.with(context).load(url).into(holder.imageYouWantToShow);

now in the fragment get the urls:

 DatabaseRefernce imagesRef = FirebaseDatabase.getInstance().getReference();
 ValueEventListener imagesListner = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot imageUrl: dataSnapshot.getChildren()) {
           String image = imageUrl.getValue(String.class);
           imagesArray.add(image);
      }
  
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
    Log.w(TAG, "loadPost:onCancelled", databaseError.toException());

    }
};
imagesRef.addValueEventListener(imagesListner );

recyclerManager=new LinearLayoutManager(getContext());
youthRecycler.setLayoutManager(recyclerManager);
recyclerAdapter = new MyRecyclerAdpater(this,imagesArray);
youthRecycler.setAdapter(recyclerAdapter);

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