i'm beginner android , trying use image google places api custom listview. trying using picasso. can text no problem when i'm trying attach image url gives me "target must not null" error. comments/help/suggestions appreciated.
my custom listrow places_layout.xml:
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:weightsum="1"> <imageview android:id="@+id/placeicon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="0.10"/> <textview android:id="@+id/placeinfo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:drawablepadding="20dp" android:layout_centervertical="true" android:layout_torightof="@+id/placeicon" android:layout_toendof="@+id/placeicon" /> </relativelayout> my async postexecute code:
imageview places_icon = (imageview) findviewbyid(r.id.placeicon); venuesfound = (arraylist) parsedatafound(result); arraylist<string> venueslist = new arraylist<>(); for(int = 0; i<venuesfound.size();i++){ if(venuesfound.get(i).getimageurl() != null){ picasso.with(getapplicationcontext()) .load(venuesfound.get(i).getimageurl()) .into(places_icon); } venueslist.add(venuesfound.get(i).getname() + "\nopen: " + venuesfound.get(i).getopennow() + "\n(" + venuesfound.get(i).getcategory() + ")"); } placeslist = (listview) findviewbyid(r.id.places_list); placesadapter = new arrayadapter(displayplacesactivity.this, r.layout.places_layout, r.id.placeinfo, venueslist); placeslist.setadapter(placesadapter); logcat:
java.lang.illegalargumentexception: target must not null. @ com.squareup.picasso.requestcreator.into(requestcreator.java:618) @ com.squareup.picasso.requestcreator.into(requestcreator.java:601) @ com.example.johnchy.samplegui.displayplacesactivity$datarequest.onpostexecute(displayplacesactivity.java:102) @ com.example.johnchy.samplegui.displayplacesactivity$datarequest.onpostexecute(displayplacesactivity.java:56) @ android.os.asynctask.finish(asynctask.java:631) @ android.os.asynctask.access$600(asynctask.java:177) @ android.os.asynctask$internalhandler.handlemessage(asynctask.java:644) @ android.os.handler.dispatchmessage(handler.java:99) @ android.os.looper.loop(looper.java:137) @ android.app.activitythread.main(activitythread.java:5419) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:525) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1187) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:1003) @ dalvik.system.nativestart.main(native method) again, appreciated!
the problem is, try attach image imageview, not there yet. since want show image in list, need move attachment adapter, since create new imageview. need create new arrayadapter purpose:
public class picassoadapter extends arrayadapter<string> { public picassoadapter(list<string> urls, context context){ super(context, 0, urls); } @override public view getview(int position, view convertview, viewgroup parent) { placeviewholder holder; //listview tries reuse invisible row-views save memory, thats why method can called null or ready view if(convertview == null){ //in case need create new view //create holder object attach view holder = new placeviewholder(); //in line create new row xml-file convertview = view.inflate(getcontext(), r.layout.places_layout, parent, false); //we attach image- , text-view our holder, can access them in next step holder.placeicon = (imageview)convertview.findviewbyid(r.id.placeicon); holder.placeinfo = (textview)convertview.findviewbyid(r.id.placeinfo); convertview.settag(holder) } else { //if view created, holder-object holder = (placeviewholder)convertview.gettag(); } //i assume url string, if need type, change here , in class declaration string url = getitem(position); picasso.with(getcontext()) .load(url) //now have imageview, can use target .into(holder.placeicon); //you can set info here holder.placeinfo.settext("test"); } } //we need class holder object public static class placeviewholder { private imageview placeicon; private textview palceinfo; } } now can use adapter in list:
placeslist.setadapter(new picassoadapter(urls, displayplacesactivity.this)); beware, performance of code may bad, since try load image on ui-thread, think it's ok example
Comments
Post a Comment