Staggered Grid Layout used to load the images from Real time database not showing

198 Views Asked by At

I have created a Staggered Grid Layout to load the images from Firebase but it is not working properly. Loaded the images using picasso and used realtime database to read the items... Showing no error in the Log cat when I use the Log. Still shows the blank page as a blank activity

// private List<PostItem> postItems;
public static List<PostItem> postItems;

//////////////
private OnItemClickListener mListener;
public interface OnItemClickListener {
    void onItemClick(int position);

}
public void setOnItemClickListener(OnItemClickListener listener) {
    mListener = listener;
}

///////////////

public PostsAdapter(List<PostItem> postItems) {
    PostsAdapter.postItems = postItems;
    
}

@NonNull
@Override
public PostViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    return new PostViewHolder(
            LayoutInflater.from(parent.getContext()).inflate(
                    R.layout.post_item_container,parent,false

            )
    );

}

@Override
public void onBindViewHolder(@NonNull PostViewHolder holder, int position) {
    holder.setPostImage(postItems.get(position));
    
}

@Override
public int getItemCount() {
    return postItems.size();

}

class PostViewHolder extends RecyclerView.ViewHolder{

    RoundedImageView postImageView;


    PostViewHolder(@NonNull View itemView) {
        super(itemView);
        
        postImageView = itemView.findViewById(R.id.imagePost);


        ///////
        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    int position = getAdapterPosition();

                    //if (position != RecyclerView.NO_POSITION) {
                    //   mListener.onItemClick(position);
                    //}
                }                 
            });
            ///////
        }
        void setPostImage(PostItem postItem){
            //load image here in picasso.
           
            Picasso.get().load(postItem.getPit()).into(postImageView);
           // postImageView.setImageResource(postItem.getImage());
        }
    }
}

The above code used in the PostsAdapter.class and there is also another class for PostItem.class and the below code is used to read the data and write to array of list - gallery.java:

public class Gallery extends AppCompatActivity {

    DatabaseReference databaseL;
    PostItem postItem;
    public static List<PostItem> postItems;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gallery);
        //
        databaseL = FirebaseDatabase.getInstance().getReference("galumon");
        postItem = new PostItem();

        final RecyclerView postRecyclerView = findViewById(R.id.postRecyclerview);
        postRecyclerView.setLayoutManager(
                new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL)
        );
        postItems = new ArrayList<>();
        /*
        List<PostItem> postItems = new ArrayList<>();
        postItems.add(new PostItem(R.drawable.a));
        postItems.add(new PostItem(R.drawable.b));
        postItems.add(new PostItem(R.drawable.c));
        postItems.add(new PostItem(R.drawable.d));
        postItems.add(new PostItem(R.drawable.e));
        postItems.add(new PostItem(R.drawable.f));
        postItems.add(new PostItem(R.drawable.g));
        postItems.add(new PostItem(R.drawable.h));
        postItems.add(new PostItem(R.drawable.i));

         */
        //
        databaseL.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {

                for (DataSnapshot ds : snapshot.getChildren()) {
                    postItem = ds.getValue(PostItem.class);
                    postItems.add(new PostItem(postItem.getPit()));
                    
                }

            }

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

            }
        });

        postRecyclerView.setAdapter(new PostsAdapter(postItems));
        

    }
}
1

There are 1 best solutions below

0
On

It's because you're setting the adapter to RecyclerView first and updating the list later.

I assume earlier it was working when you manually added images in the list and set the adapter (referring to commented code).

Try this:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_gallery);
    //
    databaseL = FirebaseDatabase.getInstance().getReference("galumon");
    postItem = new PostItem();

    final RecyclerView postRecyclerView = findViewById(R.id.postRecyclerview);
    postRecyclerView.setLayoutManager(
            new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL)
    );
    postItems = new ArrayList<>();
    
    //set the adapter 
    PostsAdapter postsAdapter = new PostsAdapter(postItems);
    postRecyclerView.setAdapter(postsAdapter);

    /*
    List<PostItem> postItems = new ArrayList<>();
    postItems.add(new PostItem(R.drawable.a));
    postItems.add(new PostItem(R.drawable.b));
    postItems.add(new PostItem(R.drawable.c));
    postItems.add(new PostItem(R.drawable.d));
    postItems.add(new PostItem(R.drawable.e));
    postItems.add(new PostItem(R.drawable.f));
    postItems.add(new PostItem(R.drawable.g));
    postItems.add(new PostItem(R.drawable.h));
    postItems.add(new PostItem(R.drawable.i));

     */
    //
    databaseL.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {

            for (DataSnapshot ds : snapshot.getChildren()) {
                postItem = ds.getValue(PostItem.class);
                postItems.add(new PostItem(postItem.getPit()));
            }

            //notify the adapter
            postsAdapter.notifyDataSetChanged();
        }

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

        }
    }); 
}