毕加索自定义列表视图

时间:2015-07-20 21:19:00

标签: java android listview imageview picasso

我是Android的初学者,我正在尝试使用google places API中的图片到我的自定义列表视图。我试图用毕加索做这件事。我可以得到文本没有问题但是当我试图从网址附加图像时它给了我一个"目标不能为空"错误。任何意见/帮助/建议表示赞赏。

我的自定义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代码:

    ImageView places_icon = (ImageView) findViewById(R.id.placeicon);

    venuesfound = (ArrayList) parsedataFound(result);
            ArrayList<String> venuesList = new ArrayList<>();
            for(int i = 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 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)

再次,感谢任何帮助!

1 个答案:

答案 0 :(得分:2)

问题是,您尝试将图像附加到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;
    }
}

现在您可以在列表中使用此适配器:

placesList.setAdapter(new PicassoAdapter(urls, DisplayPlacesActivity.this));
请注意,此代码的性能可能非常糟糕,因为我们尝试在UI-Thread上加载图像,但我认为它可以作为示例