I have already tried various ways to connect the Firebase database to RecyclerView, but every time when the application is launched, it refuses to show pictures from the repository. And while there are no errors, just open an empty Activity. How to cope with this problem? Data structure in Firebase database (images are transferred from Firebase storage) enter image description here

Activity code

public class SimpleActivity extends AppCompatActivity { private ArrayList<GalleryGridObject> galleryList = new ArrayList<>(); private RecyclerView recyclerView; private DatabaseReference mRef; private FirebaseDatabase database; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_simple); recyclerView = findViewById(R.id.my_recycler_view); recyclerView.setHasFixedSize(true); database = FirebaseDatabase.getInstance(); //mRef = mFirebaseDatabase.getReference().child("Data"); mRef = database.getReference("Data"); FirebaseRecyclerOptions<GalleryGridObject> options = new FirebaseRecyclerOptions.Builder<GalleryGridObject>() .setQuery(mRef, GalleryGridObject.class) .build(); FirebaseRecyclerAdapter<GalleryGridObject, ViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<GalleryGridObject, ViewHolder>(options) { @Override protected void onBindViewHolder(@NonNull ViewHolder holder, int position, @NonNull GalleryGridObject model) { Picasso.get().load(model.getImage()).into(holder.imageGallery); } @NonNull @Override public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) { View itemView = LayoutInflater.from(viewGroup.getContext()) .inflate(R.layout.item_coupons, viewGroup, false); return new ViewHolder(itemView); } }; LinearLayoutManager layoutManager = new LinearLayoutManager(getApplicationContext()); layoutManager.setOrientation(LinearLayoutManager.VERTICAL); recyclerView.setLayoutManager(layoutManager); firebaseRecyclerAdapter.startListening(); recyclerView.setAdapter(firebaseRecyclerAdapter); } } 

Model for RecyclerView GalleryGridObject

 @IgnoreExtraProperties 

public class GalleryGridObject {

 String image; public GalleryGridObject () { // Default constructor required for calls to DataSnapshot.getValue(User.class) } public GalleryGridObject(String image) { this.image = image; } public String getImage() { return image; } public void setImage(String image) { this.image = image; } } 

ViewHolder for RecyclerView

 public class ViewHolder extends RecyclerView.ViewHolder { public ImageView imageGallery; public ViewHolder(@NonNull View itemView) { super(itemView); imageGallery = itemView.findViewById(R.id.coupons_picture); } } 

    2 answers 2

    Well, I look at your code and the question arises.

    Here you have created an array (List), but you do not add anywhere in the code, the data in this array. And then you want to get data from this empty array.

    enter image description here

    As an answer, I can show an example:

      private List<String> list_img = new ArrayList<String>(); private DatabaseReference mRef; private FirebaseDatabase database; database = FirebaseDatabase.getInstance(); myRef = database.getReference(); myRef.addListenerForSingleValueEvent(new ValueEventListener() { @Override public void onDataChange(@NonNull DataSnapshot dataSnapshot) { for (DataSnapshot ds : dataSnapshot.child("Data").getChildren()){ list_img.add(String.valueOf(dataSnapshot.child("Data").child(ds.getKey()).getValue())); } } @Override public void onCancelled(@NonNull DatabaseError databaseError) { } }); 
    • galleryList remains from the previous method, but in my code it is not used anywhere, instead I use GalleryGridObject.class - 3Jlou 4uTep 2:22 pm
    • In your example, you use String instead of a custom array, and how to create an adapter with it if there is no getting and setting for, for example, connecting Picasso (getImage () method) - (Picasso.get (). Load (galleryList.getImage ()). into (holder.imageGallery); - 3Jlou 4uTep
    • used your method, but now only 1 CardView appeared in RecyclerView, and then it is completely empty - 3Jlou 4uTep

    You need to change the private ArrayList<GalleryGridObject> galleryList = new ArrayList<>(); on the private List<GalleryGridObject> galleryList; As a result, the fragment code (or activation) should be such

     public class TestFragment extends Fragment { private List<GalleryGridObject> galleryList; private TestAdapter mAdapter; private RecyclerView recyclerView; private DatabaseReference mRef; private FirebaseDatabase database; public TestFragment() { // Required empty public constructor } @Override public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View view = inflater.inflate(R.layout.fragment_coupons, container, false); recyclerView = view.findViewById(R.id.my_recycler_view); recyclerView.setHasFixedSize(true); LinearLayoutManager layoutManager = new LinearLayoutManager(getContext()); layoutManager.setOrientation(LinearLayoutManager.VERTICAL); recyclerView.setLayoutManager(layoutManager); galleryList = new ArrayList<>(); mAdapter = new TestAdapter(galleryList); database = FirebaseDatabase.getInstance(); //mRef = database.getReference().child("Data"); mRef = database.getReference("Data"); mRef.addChildEventListener(new ChildEventListener() { @Override public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) { GalleryGridObject image = dataSnapshot.getValue(GalleryGridObject.class); galleryList.add(image); recyclerView.setAdapter(mAdapter); } @Override public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) { } @Override public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) { } @Override public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) { } @Override public void onCancelled(@NonNull DatabaseError databaseError) { } }); return view; } public class TestAdapter extends RecyclerView.Adapter<TestAdapter.ViewHolder>{ private Context mContext; private List<GalleryGridObject> galleryArrayList; public TestAdapter( List<GalleryGridObject> galleryArrayList){ //this.mContext=mContext; this.galleryArrayList=galleryArrayList; } @NonNull @Override public TestAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View itemView = LayoutInflater.from(parent.getContext()) .inflate(R.layout.item_coupons, parent, false); return new TestAdapter.ViewHolder(itemView); } @Override public void onBindViewHolder(@NonNull TestAdapter.ViewHolder holder, int position) { //GalleryGridObject gallery = galleryArrayList.get(position); //Glide.with(mContext).load(galleryArrayList.get(position).getImage()).into(holder.imageGallery); holder.textView.setText(galleryArrayList.get(position).getTitle()); Picasso.get().load(galleryArrayList.get(position).getImage()).into(holder.imageGallery); } @Override public int getItemCount() { if(galleryArrayList==null) return 0; return galleryArrayList.size(); } public class ViewHolder extends RecyclerView.ViewHolder { public ImageView imageGallery; public TextView textView; public ViewHolder(View view) { super(view); imageGallery = itemView.findViewById(R.id.coupons_picture); textView = itemView.findViewById(R.id.coupons_title); } } } } 

    Here is the model for RecyclerView (an empty constructor is required)

     @IgnoreExtraProperties public class GalleryGridObject { String title, image; public GalleryGridObject () { // Default constructor required for calls to DataSnapshot.getValue(User.class) } public GalleryGridObject(String title, String image) { this.title = title; this.image = image; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getImage() { return image; } public void setImage(String image) { this.image = image; } }