David
David

Reputation: 25

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>

Upvotes: 0

Views: 633

Answers (3)

Syed Bilal
Syed Bilal

Reputation: 91

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

Upvotes: 0

Malik Saifullah
Malik Saifullah

Reputation: 584

The URL you are trying to load is a redirecting to the actual website not the original image.

Try this link of the same image

I got this URL by inspecting element.

Upvotes: 1

Mike
Mike

Reputation: 1343

The URL seems to be redirecting to wikihow website instead of returning raw JPG.

https://www.wikihow.com/images/thumb/4/44/Get-the-URL-for-Pictures-Step-6-Version-3.jpg/v4-760px-Get-the-URL-for-Pictures-Step-6-Version-3.jpg

Upvotes: 1

Related Questions