Implementing Picasso not loading image

Skizo-ozᴉʞS

After I decided to implement Universal Image Loader, because I had implemented a method that convert URL to Drawable, but since I don't know how many images it will return my SQLite query I decided to implement an Image Loader... The thing is I'm stuck at the moment, cause I thought I did all what the GitHub say but at the time I load the Image it stays white and never loads.

On my Adapter class I've changed the line of the drawable as :

Picasso.with(context)
            .load(Uri.parse(String.valueOf(item.icon)))
            .resize(180, 180)
            .placeholder(R.drawable.ic_launcher).into(viewHolder.ivIcon);

It works, beucase it shows yo me the ic_launcher icon... but never changes to the real image.

On my class where I fetch the data I have this (on my OnCreate()) :

   new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(2000);


                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    new MyAsyncTask().execute();

                    getActivity().runOnUiThread(new Runnable() {

                        @Override
                        public void run() {
                    //   progress.dismiss();
                        }
                    });
                }

            }).start();
        }

Then I created an inner class where I fetch the data into my ListView... but it doesn't works. I don't know If I've to delte those methods since I've changed it to Picasso.

private class MyAsyncTask extends AsyncTask<Void, Void, Void> {
    @Override
    protected void onPreExecute() {
    }

    @Override
    protected Void doInBackground(Void... params) {

        Conexion = new MarketSQLite(getActivity(), "market", null, 1);
        mItems = new ArrayList<ListViewItem>();

        db = Conexion.getReadableDatabase();

        Cursor c;

        c = db.rawQuery("Select NOM_OFER,PREU_OFERTA,DATA_F,FOTO,PERCENTDESCOMPTE from T_OFERTA", null);
        c.moveToFirst();
        if (c != null) {
            do {
                for (int i = 0; i < c.getColumnCount(); i++) {
                    Title = c.getString((c.getColumnIndex("NOM_OFER")));
                    Preu = c.getColumnIndex("PREU_OFERTA");
                    percent = c.getString((c.getColumnIndex("PERCENTDESCOMPTE")));
                    data_f = c.getString((c.getColumnIndex("DATA_F")));
                    URLTest = c.getString((c.getColumnIndex("FOTO")));
                    FOTO = Imagehandler(URLTest);
                    Log.e("", "" + c.getString(i));

                    // initialize and set the list adapter


                   // Toast.makeText(getActivity(), "Title" + Title + "Preu" + Preu + "Percent" + percent + "Cheese is " + data_f, Toast.LENGTH_LONG).show();
                }
                mItems.add(new ListViewItem(FOTO, Title, Preu.toString(), percent, data_f));


            }while (c.moveToNext());
        }
        c.close();

return null;
    }


    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        myAdapter = new ListViewDemoAdapter(getActivity(), mItems);
        setListAdapter(myAdapter);
    }
}

Where ImageHandler is a method that I've created before this is :

 protected Drawable Imagehandler(String url) {
        try {
            url=url.replaceAll(" ", "%20");
            InputStream is = (InputStream)this.fetch(url);
            Drawable d = Drawable.createFromStream(is, "src");
            return d;
        } catch (MalformedURLException e)
        {
            System.out.println(url);
            System.out.println("error at URI"+e);
            return null;
        }
        catch (IOException e)
        {
            System.out.println("io exception: "+e);
            System.out.println("Image NOT FOUND");
            return null;
        }
    }

    protected Object fetch(String address) throws MalformedURLException,IOException {
        URL url = new URL(address);
        Object content = url.getContent();
        return content;
    }

I don't know why isn't the image loading on my ListView if it shows all of the rest of data...

Narendra Singh

Instead of Drawable, try to get url string in your adapter like

Change From

public ListViewItem(Drawable icon, String title, String precio, String descuento, String date) {
    this.icon = icon;
    this.title = title;
    this.precio = precio;
    this.descuento = descuento;
    this.date = date;
}

To

    public ListViewItem(String icon_url, String title, String precio, String descuento, String date) {
        this.icon_url = icon_url;
        this.title = title;
        this.precio = precio;
        this.descuento = descuento;
        this.date = date;
    }

and use Picasso where you are loading your imageview like this -

Picasso.with(context)
            .load(icon_url))
            .resize(180, 180)
            .placeholder(R.drawable.ic_launcher).into(viewHolder.ivIcon);

1) Your ListViewItem class should be like this -

public class ListViewItem {
    public final String icon;       // the drawable for the ListView item ImageView
    public final String title;       // the text for the ListView item title
    public final String precio;      // the price for the ListView item
    public final String descuento;   // the price for the discount for the ListView item
    public final String date;        //the date for the sale for the ListView item

     // the text for the ListView item description

    public ListViewItem(String icon_url, String title, String precio, String descuento, String date) {
        this.icon = icon_url;
        this.title = title;
        this.precio = precio;
        this.descuento = descuento;
        this.date = date;
    }
}

2) ListViewDemoAdapterClass

public class ListViewDemoAdapter extends ArrayAdapter<ListViewItem> {
Context context;
    public ListViewDemoAdapter(Context context, List<ListViewItem> items) {
        super(context, R.layout.listview_item, items);
        this.context = context;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;

        if(convertView == null) {
            // inflate the GridView item layout
            LayoutInflater inflater = LayoutInflater.from(getContext());
            convertView = inflater.inflate(R.layout.listview_item, parent, false);

            // initialize the view holder
            viewHolder = new ViewHolder();
            viewHolder.ivIcon = (ImageView) convertView.findViewById(R.id.ivIcon);
            viewHolder.tvTitle = (TextView) convertView.findViewById(R.id.tvTitle);
            viewHolder.tvPrice = (TextView) convertView.findViewById(R.id.tvPrice);
            viewHolder.tvDiscount = (TextView) convertView.findViewById(R.id.tvDiscount);
            viewHolder.tvDate = (TextView) convertView.findViewById(R.id.tvDatas);
            convertView.setTag(viewHolder);
        } else {
            // recycle the already inflated view
            viewHolder = (ViewHolder) convertView.getTag();
        }

        // update the item view
        ListViewItem item = getItem(position);
        Picasso.with(context)
                .load(item.icon)
                .resize(180, 180)
                .placeholder(R.drawable.ic_launcher).into(viewHolder.ivIcon);
        viewHolder.tvTitle.setText(item.title);
        viewHolder.tvDiscount.setText(item.descuento);
        viewHolder.tvPrice.setText(item.precio);
        viewHolder.tvDate.setText(item.date);

        return convertView;
    }


    private static class ViewHolder {
        ImageView ivIcon;
        TextView tvTitle;
        TextView tvDiscount;
        TextView tvPrice;
        TextView tvDate;
    }
}
  1. ListFragment code, just add this

      Cursor c; 
    
     c = db.rawQuery("Select 
    NOM_OFER,PREU_OFERTA,DATA_F,FOTO,PERCENTDESCOMPTE from T_OFERTA", null); 
    c.moveToFirst(); 
    if (c != null) { 
    do { 
    for (int i = 0; i < c.getColumnCount(); i++) { 
    Title = c.getString((c.getColumnIndex("NOM_OFER"))); 
    Preu = c.getColumnIndex("PREU_OFERTA"); 
    percent = c.getString((c.getColumnIndex("PERCENTDESCOMPTE"))); 
    data_f = c.getString((c.getColumnIndex("DATA_F"))); 
    URLTest = c.getString((c.getColumnIndex("FOTO")));
    

Hope this helps :)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Picasso not loading the image

From Dev

Picasso not loading URL into image

From Dev

picasso android image not loading

From Dev

Image loading using picasso on disk

From Dev

How to delay Picasso image loading?

From Dev

Image Slider image not loading (ViewPager + Picasso)

From Dev

Fade in animation while loading image Using Picasso

From Dev

Loading image using Picasso showing colors in corners

From Dev

Picasso image loading previously cached images

From Dev

Picasso not loading updated image from Web in android

From Java

Picasso image loading issue with Android 9.0 Pie

From Dev

Picasso loading of image spawned inside AsyncTask

From Dev

Picasso's image loading using fit() method

From Dev

Android,Picasso. Is not loading an Image from a url

From Dev

Picasso loading image error-Android

From Dev

Picasso image loading issue in GoogleMap.InfoWindowAdapter

From Dev

Picasso only loading one image in a BaseAdapter

From Dev

Picasso image loading issue with Android 9.0 Pie

From Dev

Picasso Image Loading Library, some issues

From Dev

Android: Lazy Loading Image in horizontalscrollview using Picasso

From Dev

Picasso loading image error-Android

From Dev

Android - Picasso removes margin after loading Image

From Dev

Picasso only loading single image but the size is 20

From Dev

Different width when loading same image with Picasso

From Dev

Picasso not loading image from a URL, sometimes

From Dev

Show an "Loading..." image while background loading of image with Picasso

From Dev

Show an "Loading..." image while background loading of image with Picasso

From Dev

Picasso image loader: loading updated images from url

From Dev

Synchronous image loading on a background thread with Picasso - without .get()

Related Related

  1. 1

    Picasso not loading the image

  2. 2

    Picasso not loading URL into image

  3. 3

    picasso android image not loading

  4. 4

    Image loading using picasso on disk

  5. 5

    How to delay Picasso image loading?

  6. 6

    Image Slider image not loading (ViewPager + Picasso)

  7. 7

    Fade in animation while loading image Using Picasso

  8. 8

    Loading image using Picasso showing colors in corners

  9. 9

    Picasso image loading previously cached images

  10. 10

    Picasso not loading updated image from Web in android

  11. 11

    Picasso image loading issue with Android 9.0 Pie

  12. 12

    Picasso loading of image spawned inside AsyncTask

  13. 13

    Picasso's image loading using fit() method

  14. 14

    Android,Picasso. Is not loading an Image from a url

  15. 15

    Picasso loading image error-Android

  16. 16

    Picasso image loading issue in GoogleMap.InfoWindowAdapter

  17. 17

    Picasso only loading one image in a BaseAdapter

  18. 18

    Picasso image loading issue with Android 9.0 Pie

  19. 19

    Picasso Image Loading Library, some issues

  20. 20

    Android: Lazy Loading Image in horizontalscrollview using Picasso

  21. 21

    Picasso loading image error-Android

  22. 22

    Android - Picasso removes margin after loading Image

  23. 23

    Picasso only loading single image but the size is 20

  24. 24

    Different width when loading same image with Picasso

  25. 25

    Picasso not loading image from a URL, sometimes

  26. 26

    Show an "Loading..." image while background loading of image with Picasso

  27. 27

    Show an "Loading..." image while background loading of image with Picasso

  28. 28

    Picasso image loader: loading updated images from url

  29. 29

    Synchronous image loading on a background thread with Picasso - without .get()

HotTag

Archive