Search code examples
androidfirebaseandroid-recyclerviewfragmentpicasso

Picasso does not load image in fragment with RecyclerView


I am trying to use Picasso to load an image in fragment and Recyclerview. But I can't load a picture. Initially I was thinking maybe it couldn't get the URL from DB but from the debug I could see the correct URL. I tried hard coded URL but it is still not working. Hope experts can help me out.

This is the code:


public class MenuFragment extends Fragment {

   private MenuViewModel menuViewModel;

   FirebaseDatabase database;
   DatabaseReference category;
   RecyclerView recycler_menu;
   RecyclerView.LayoutManager layoutManager;

   public View onCreateView(@NonNull LayoutInflater inflater,
                            ViewGroup container, Bundle savedInstanceState) {

       //init fireabse
       database = FirebaseDatabase.getInstance();
       category = database.getReference("Category");

       View root = inflater.inflate(R.layout.fragment_memu, container, false);
       recycler_menu = (RecyclerView) root.findViewById(R.id.recycler_menu);
       recycler_menu.setHasFixedSize(true);
       recycler_menu.setLayoutManager(new LinearLayoutManager(inflater.getContext()));

       loadMenu();
       //recycler_menu.setAdapter(new ChatRecyclerViewAdapter());

       return root;
   }

   private void loadMenu() {
       FirebaseRecyclerAdapter<Category,MenuViewHolder> adapter = new FirebaseRecyclerAdapter<Category, MenuViewHolder>(
               Category.class, R.layout.menu_item, MenuViewHolder.class, category) {
           @Override
           protected void populateViewHolder(MenuViewHolder viewHolder, Category category, int i) {
               viewHolder.txtMenuName.setText(category.getName());
               Picasso.with(getContext()).load("https://www.wikihow.com/Get-the-URL-for-Pictures#/Image:Get-the-URL-for-Pictures-Step-6-Version-3.jpg").
                      fit().centerCrop().into(viewHolder.imageView);

               final Category clickItem = category;
               viewHolder.setItemClickListener(new ItemClickListener() {
                   @Override
                   public void onClick(View v, int position, boolean isLongClick) {
                       Toast.makeText(getActivity().getBaseContext(),""+clickItem.getName(),Toast.LENGTH_SHORT).show();
                   }
               });

           }
       };
       recycler_menu.setAdapter((adapter));
   }
}

This is XML part

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.menu.MenuFragment">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler_menu"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical">
    </androidx.recyclerview.widget.RecyclerView>

</androidx.constraintlayout.widget.ConstraintLayout>

Solution

  • Picasso.with(getContext()).load("Image_url").fit().centerCrop().into(viewHolder.imageView);

    I've checked the url you used in load() method and this isn't an image url. When I've opened this url websites has been appeared rather than open an image.

    Kindly use this url