2015-07-20 41 views
1

我是一個初學者與Android,我試圖從谷歌地方API使用圖像到我的自定義列表視圖。我試圖用畢加索來做到這一點。我可以讓文本沒有問題,但是當我試圖從URL附加圖像時,它給了我一個「目標不能爲空」的錯誤。任何意見/幫助/建議表示讚賞。畢加索對自定義列表視圖

我定製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> 

我的異步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) 

同樣,任何幫助表示讚賞!

+0

其中是'com.example.johnchy.samplegui.DisplayPlacesActivity $ dataRequest.onPostExecute(DisplayPlacesActivity.java:102)'? – JohnnyAW

+0

感謝您的回覆!我相信這是Async onPostExecute代碼上的Picasso行:Picasso.with(getApplicationContext()) .load(venuesfound.get(i).getImageURL()) .into(places_icon);它在第二塊代碼 – Masterofawesome

+0

上,然後用你的調試器檢查這些值,在這一行中有一些是'null'。我猜'places_icon'爲空,因爲它是目標 – JohnnyAW

回答

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線程上加載圖像,但我認爲它可以作爲示例

+0

謝謝!工作!但是你是對的,它確實減慢了我的應用程序。你對如何提高效率有什麼建議嗎? – Masterofawesome

+0

您可以嘗試在異步調用中加載圖像,並將圖標作爲byte []放入List中,並使用該列表而不是Url調用適配器。我對畢加索不熟悉,但我想他們有一些功能可以將圖標變爲'byte []'。即使如此,在屏幕上顯示多個大圖像對於中低價位的智能手機來說是一項沉重的任務 – JohnnyAW

+0

謝謝!我會嘗試。 – Masterofawesome