Java 定制listview上的毕加索
我是一个android的初学者,我正在尝试将google places API中的图像用于我的自定义listview。我正试图用毕加索来做这件事。我可以得到文本没有问题,但当我试图从url附加图像时,它会给我一个“Target must not null”错误。如有任何意见/帮助/建议,我们将不胜感激 我的自定义listrow places_layout.xml:Java 定制listview上的毕加索,java,android,listview,imageview,picasso,Java,Android,Listview,Imageview,Picasso,我是一个android的初学者,我正在尝试将google places API中的图像用于我的自定义listview。我正试图用毕加索来做这件事。我可以得到文本没有问题,但当我试图从url附加图像时,它会给我一个“Target must not null”错误。如有任何意见/帮助/建议,我们将不胜感激 我的自定义listrow places_layout.xml: <?xml version="1.0" encoding="utf-8"?> <Relativ
<?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>
再次感谢您的帮助 问题是,您试图将图像附加到一个尚未存在的
ImageView
。由于要在列表中显示图像,因此需要将附件移动到适配器中,因为这是创建新的ImageView
的地方。为此,您需要创建一个新的ArrayAdapter
:
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 to reuse invisible Row-Views to save memory, thats why this method can be called with null OR actually a ready View
if(convertView == null){
//in this case we need to create a new View
//create a holder Object that we will attach to the view
holder = new PlaceViewHolder();
//in this line we actually create a new row from the xml-file
convertView = View.inflate(getContext(), R.layout.places_layout, parent, false);
//we attach the Image- and Text-View to our holder, so we can access them in the next step
holder.placeIcon = (ImageView)convertView.findViewById(R.id.placeicon);
holder.placeInfo = (TextView)convertView.findViewById(R.id.placeinfo);
convertView.setTag(holder)
} else {
//if the view is already created, simply get the holder-object
holder = (PlaceViewHolder)convertView.getTag();
}
//I assume the URL is a String, if you need another Type, simply change it here and in class declaration
String url = getItem(position);
Picasso.with(getContext())
.load(url)
//now we have an ImageView, that we can use as target
.into(holder.placeIcon);
//you can set the info here
holder.placeInfo.setText("test");
}
}
//we need this class as holder object
public static class PlaceViewHolder {
private ImageView placeIcon;
private TextView palceInfo;
}
}
请注意,这段代码的性能可能非常差,因为我们试图在UI线程上加载图像,但我认为作为一个示例,这是可以的,其中是
com.example.johnchy.samplegui.DisplayPlacesActivity$dataRequest.onPostExecute(DisplayPlacesActivity.java:102)
?感谢您的响应!我相信这就是毕加索在异步onPostExecute代码上的行:picasso.with(getApplicationContext()).load(venuesfound.get(I.getImageURL()).into(places_图标);它位于第二个代码块上,然后使用调试器检查值,这一行中有null
。我猜,places\u icon
是空的,因为它就是我发现的奇怪的目标。第二段代码的第一行是实例化logcat所说的null。我不知道为什么它会突然失去参考。或者我看得不对?我想问题是,您试图在活动视图中找到相应的元素,因为您使用的是活动方法。您需要使用已膨胀的列表行places\u layout.xml
视图上的findViewById
,谢谢!成功了!但你是对的,它确实让我的应用程序慢了一点。您对如何提高效率有什么建议吗?您可以尝试在异步调用中加载图像,并将图标作为byte[]
放入列表
中,然后使用该列表而不是Url调用适配器。我不熟悉毕加索,但我想他们有一些功能来获得一个图标,如byte[]
。即使如此,在屏幕上显示多个大图像对于中低价智能手机来说也是一项繁重的任务。谢谢!我试试看。
java.lang.IllegalArgumentException: Target must not be null.
at com.squareup.picasso.RequestCreator.into(RequestCreator.java:618)
at com.squareup.picasso.RequestCreator.into(RequestCreator.java:601)
at com.example.johnchy.samplegui.DisplayPlacesActivity$dataRequest.onPostExecute(DisplayPlacesActivity.java:102)
at com.example.johnchy.samplegui.DisplayPlacesActivity$dataRequest.onPostExecute(DisplayPlacesActivity.java:56)
at android.os.AsyncTask.finish(AsyncTask.java:631)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5419)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
at dalvik.system.NativeStart.main(Native Method)
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 to reuse invisible Row-Views to save memory, thats why this method can be called with null OR actually a ready View
if(convertView == null){
//in this case we need to create a new View
//create a holder Object that we will attach to the view
holder = new PlaceViewHolder();
//in this line we actually create a new row from the xml-file
convertView = View.inflate(getContext(), R.layout.places_layout, parent, false);
//we attach the Image- and Text-View to our holder, so we can access them in the next step
holder.placeIcon = (ImageView)convertView.findViewById(R.id.placeicon);
holder.placeInfo = (TextView)convertView.findViewById(R.id.placeinfo);
convertView.setTag(holder)
} else {
//if the view is already created, simply get the holder-object
holder = (PlaceViewHolder)convertView.getTag();
}
//I assume the URL is a String, if you need another Type, simply change it here and in class declaration
String url = getItem(position);
Picasso.with(getContext())
.load(url)
//now we have an ImageView, that we can use as target
.into(holder.placeIcon);
//you can set the info here
holder.placeInfo.setText("test");
}
}
//we need this class as holder object
public static class PlaceViewHolder {
private ImageView placeIcon;
private TextView palceInfo;
}
}
placesList.setAdapter(new PicassoAdapter(urls, DisplayPlacesActivity.this));