usd123
usd123

Reputation: 37

Picasso is not loading the images from url with custom adapter

I am loading the images into my list view from custom adpater using picasso. Activity is stopping when run the code by adding picasso to the adapter. Below is my code.

ListActivity:

protected void onPostExecute(String file_url) {
            // dismiss the dialog after getting all products
            pDialog.dismiss();
            // updating UI from Background Thread
            runOnUiThread(new Runnable() {
                public void run() {
                    /**
                     * Updating parsed JSON data into ListView
                     * */

                 // updating listview
                // setListAdapter(adapter);

                 ListView list1 = (ListView)findViewById(android.R.id.list);    
                 CustomAdapter cus = new CustomAdapter(AllProductsActivity.this, productsList);       
                 list1.setAdapter(cus);


                                    }
            });

        }

Custom Adpater:

public class CustomAdapter extends BaseAdapter {
    private Context context;
    LayoutInflater mInlfater;
    ArrayList<HashMap<String,String>> productsList;


// Constructor
public CustomAdapter(Context context,ArrayList<HashMap<String,String>> productsList) 
{
     mInlfater = LayoutInflater.from(context);
     this.productsList =productsList;
}


//public ImageAdapter(MainActivity mainActivity, string[] items){
//}



@Override
public int getCount() {
    return productsList.size();

}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    ViewHolder holder;
    if(convertView ==null)
    {
        convertView = mInlfater.inflate(R.layout.list_item,parent, false);
        holder = new ViewHolder();
        holder.img1 = (TextView)convertView.findViewById(R.id.img);
        holder.nm1 = (TextView)convertView.findViewById(R.id.name);
        holder.imageView = (ImageView)convertView.findViewById(R.id.prod_image);

        convertView.setTag(holder);


    }
    else
    {
        holder =(ViewHolder) convertView.getTag();
    }
    HashMap<String,String> map = productsList.get(position);

    Picasso.with(this.context).load("http://i.imgur.com/DvpvklR.png").into(holder.imageView);

    holder.nm1.setText(map.get("name"));

    return convertView;
}
static class ViewHolder
{
    ImageView imageView;
    TextView img,img1,img2,nm1;
}
}

StackTrace:

12-25 10:14:57.730: D/AndroidRuntime(933): Shutting down VM
12-25 10:14:57.730: W/dalvikvm(933): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
12-25 10:14:57.830: E/AndroidRuntime(933): FATAL EXCEPTION: main
12-25 10:14:57.830: E/AndroidRuntime(933): java.lang.IllegalArgumentException: Context must not be null.
12-25 10:14:57.830: E/AndroidRuntime(933):  at com.squareup.picasso.Picasso$Builder.<init>(Picasso.java:635)
12-25 10:14:57.830: E/AndroidRuntime(933):  at com.squareup.picasso.Picasso.with(Picasso.java:597)
12-25 10:14:57.830: E/AndroidRuntime(933):  at com.esoftall.esoft.CustomAdapter.<init>(CustomAdapter.java:42)
12-25 10:14:57.830: E/AndroidRuntime(933):  at com.esoftall.esoft.AllProductsActivity$LoadAllProducts$1.run(AllProductsActivity.java:234)
12-25 10:14:57.830: E/AndroidRuntime(933):  at android.app.Activity.runOnUiThread(Activity.java:4644)
12-25 10:14:57.830: E/AndroidRuntime(933):  at com.esoftall.esoft.AllProductsActivity$LoadAllProducts.onPostExecute(AllProductsActivity.java:222)
12-25 10:14:57.830: E/AndroidRuntime(933):  at com.esoftall.esoft.AllProductsActivity$LoadAllProducts.onPostExecute(AllProductsActivity.java:1)
12-25 10:14:57.830: E/AndroidRuntime(933):  at android.os.AsyncTask.finish(AsyncTask.java:631)
12-25 10:14:57.830: E/AndroidRuntime(933):  at android.os.AsyncTask.access$600(AsyncTask.java:177)
12-25 10:14:57.830: E/AndroidRuntime(933):  at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
12-25 10:14:57.830: E/AndroidRuntime(933):  at android.os.Handler.dispatchMessage(Handler.java:99)
12-25 10:14:57.830: E/AndroidRuntime(933):  at android.os.Looper.loop(Looper.java:137)
12-25 10:14:57.830: E/AndroidRuntime(933):  at android.app.ActivityThread.main(ActivityThread.java:5041)
12-25 10:14:57.830: E/AndroidRuntime(933):  at java.lang.reflect.Method.invokeNative(Native Method)
12-25 10:14:57.830: E/AndroidRuntime(933):  at java.lang.reflect.Method.invoke(Method.java:511)
12-25 10:14:57.830: E/AndroidRuntime(933):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-25 10:14:57.830: E/AndroidRuntime(933):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-25 10:14:57.830: E/AndroidRuntime(933):  at dalvik.system.NativeStart.main(Native Method)

Thanks for any help!

Upvotes: 0

Views: 3345

Answers (3)

ataulm
ataulm

Reputation: 15334

It says what's wrong:

12-25 10:14:57.830: E/AndroidRuntime(933): java.lang.IllegalArgumentException: Context must not be null.
12-25 10:14:57.830: E/AndroidRuntime(933):  at com.squareup.picasso.Picasso$Builder.<init>(Picasso.java:635)
12-25 10:14:57.830: E/AndroidRuntime(933):  at com.squareup.picasso.Picasso.with(Picasso.java:597)

Context is null because you never initialise it.

public class CustomAdapter extends BaseAdapter {
    private Context context;
    LayoutInflater mInlfater;
    ArrayList<HashMap<String,String>> productsList;


    // Constructor
    public CustomAdapter(Context context,ArrayList<HashMap<String,String>> productsList) {
         this.context = context; // <--- this is missing?

         mInlfater = LayoutInflater.from(context);
         this.productsList =productsList;
    }

    ...
}

You could also grab the Context from the View instead:

Context context = holder.imageview.getContext();
String imageUrl = "http://i.imgur.com/DvpvklR.png";
Picasso.with(context).load(imageUrl).into(holder.imageView);

Upvotes: 0

emigrantdd
emigrantdd

Reputation: 154

just get a picture of your ListView

 @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View retval = LayoutInflater.from(parent.getContext()).inflate(R.layout.listview_leyout, null);
        TextView title = (TextView) retval.findViewById(R.id.title);
        image_list_icon = (ImageView)retval.findViewById(R.id.image_fromlist);
        title.setText(dataObjects[position%dataObjects.length]);

        if (title.getText() == "Text #1") {
         Picasso.with(retval.getContext()).load("http://i.imgur.com/DvpvklR.png").into(image_list_icon);
        }
        return retval;
    }

image_list_icon in my code I get an image through retval, and you have as far as I can see through the holder, try it.

Upvotes: 2

Blundell
Blundell

Reputation: 76536

Need the stacktrace to know for sure the problem, but I would recommend moving the Picasso instantiation outside of the getView method and into the constructor. This stops the heavy creation step of the object being done every time a list item is created (could be hundreds of times).

    public class CustomAdapter extends BaseAdapter {
        private final Context context;
        private final LayoutInflater mInlfater;
        private final List<HashMap<String,String>> productsList;
        private final Picasso imageLoader;

    public CustomAdapter(Context context, List<HashMap<String,String>> productsList) 
    {
         mInlfater = LayoutInflater.from(context);
         this.productsList =productsList;
         this.imageLoader = Picasso.with(this.context);
    }

    // code

    getView() {
      // code

      imageLoader.load("http://i.imgur.com/DvpvklR.png").into(holder.imageView);

      // code
    }

}

The reason being if you look at the Picasso source code, the with() method is heavy and uses syncrhonised https://github.com/square/picasso/blob/master/picasso/src/main/java/com/squareup/picasso/Picasso.java#L639

Upvotes: 0

Related Questions