2015-12-02 138 views
-1

我有這個問題,當使用畢加索依賴項提取圖像。但是,當我取其他字符串它們顯示在我的Android app.Here是我的代碼:圖片不顯示使用畢加索在android

ListAdapter.java

public class ListAdapter extends ArrayAdapter<Restaurant> { 
    public ListAdapter(Context context, int resource) { 
     super(context, resource); 
    } 
    @Override 
    public View getView(int position,View convertView, ViewGroup parent){ 
     ViewHolder holder; 
     if (convertView == null){ 
      holder = new ViewHolder(); 
      convertView = LayoutInflater.from(getContext()).inflate(R.layout.fragment_list,parent,false); 
      holder.name = (TextView)convertView.findViewById(R.id.name); 
      holder.type = (TextView)convertView.findViewById(R.id.type); 
      holder.thumbnail = (ImageView)convertView.findViewById(R.id.thumbnail); 

      convertView.setTag(holder); 
     }else{ 
      holder = (ViewHolder)convertView.getTag(); 
     } 


     holder.name.setText(getItem(position).getName()); 
     holder.type.setText(getItem(position).getType()); 
     Picasso.with(getContext()).load(getItem(position).getThumbnail()).into(holder.thumbnail); 

     return convertView; 
    } 
    class ViewHolder{ 
     ImageView thumbnail; 
     TextView name; 
     TextView type; 
    } 
} 

當我改變爲getItem(位置).getThumbnail()爲 「http://some_image_here.png」 我可以看到圖片。

Restaurant.java

public class Restaurant implements Parcelable { 
    private String name; 
    private String type; 
    private String description; 
    private String thumbnail; 
    private String image; 

    public Restaurant(){ 

    } 
    public Restaurant(String name, String type, String description, String thumbnail, String image){ 
     this.name = name; 
     this.type = type; 
     this.description = description; 
     this.thumbnail = thumbnail; 
     this.image = image; 
    } 
    public Restaurant(Parcel source){ 
     name = source.readString(); 
     type = source.readString(); 
     description = source.readString(); 
     thumbnail = source.readString(); 
     image = source.readString(); 
    } 
    public String getImage() { 
     return image; 
    } 

    public void setImage(String image) { 
     this.image = image; 
    } 

    public String getThumbnail() { 
     return thumbnail; 
    } 

    public void setThumbnail(String thumbnail) { 

     this.thumbnail = thumbnail; 
    } 

    public String getDescription() { 

     return description; 
    } 

    public void setDescription(String description) { 

     this.description = description; 
    } 

    public String getType() { 
     return type; 
    } 

    public void setType(String type) { 
     this.type = type; 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    @Override 
    public int describeContents() { 
     return 0; 
    } 

    @Override 
    public void writeToParcel(Parcel dest, int flags) { 
     applyDefaultValues(); 

     dest.writeString(name); 
     dest.writeString(type); 
     dest.writeString(description); 
     dest.writeString(image); 
     dest.writeString(thumbnail); 
    } 

    private void applyDefaultValues() { 
     if(name == null) 
      name=""; 
     if(type == null) 
      type =""; 
     if(description == null) 
      description = ""; 
     if(image == null) 
      image = ""; 
     if(thumbnail == null) 
      thumbnail = ""; 
    } 
    public static Creator<Restaurant>CREATOR = new Creator<Restaurant>() { 
     @Override 
     public Restaurant createFromParcel(Parcel source) { 

      return new Restaurant(source); 
     } 

     @Override 
     public Restaurant[] newArray(int size) { 

      return new Restaurant[size]; 
     } 
    }; 
} 

這裏是我的fragment_list.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
xmlns:tools="http://schemas.android.com/tools" 
android:orientation="horizontal" 
tools:context="com.findmerest.fragment.ListRestFragment"> 

<ImageView 
    android:id="@+id/thumbnail" 
    android:layout_width="64dp" 
    android:layout_height="64dp"/> 

<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 

    <TextView 
     android:id="@+id/name" 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content"/> 

    <TextView 
     android:id="@+id/type" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

</LinearLayout> 

代碼有人可以告訴我什麼是錯在我的代碼?

我包括ListRestFragment:

public class ListRestFragment extends ListFragment { 

private ListAdapter mAdapter; 
public static ListRestFragment getInstance(){ 
    ListRestFragment fragment = new ListRestFragment(); 
    return fragment; 
} 
@Override 
public void onActivityCreated(@Nullable Bundle savedInstanceState){ 
    super.onActivityCreated(savedInstanceState); 

    setListShown(false); 
    mAdapter = new ListAdapter(getActivity(),0); 

    RestAdapter adapter = new RestAdapter.Builder() 
      .setEndpoint(getString(R.string.restaurant_list)) 
      .build(); 
    RestaurantApiInterface restaurantApiInterface = adapter.create(RestaurantApiInterface.class); 
    restaurantApiInterface.getStreams(new Callback<List<Restaurant>>() { 
     @Override 
     public void success(List<Restaurant> restaurants, Response response) { 
      if (restaurants == null || restaurants.isEmpty()) { 
       return; 
      } 
      for (Restaurant restaurant : restaurants) { 
       mAdapter.add(restaurant); 
      } 
      mAdapter.notifyDataSetChanged(); 
      setListAdapter(mAdapter); 
      setListShown(true); 
     } 

     @Override 
     public void failure(RetrofitError error) { 
      Log.e("Restaurant", "error" + error.getMessage()); 
     } 
    }); 
} 

@Override 
public void onListItemClick(ListView l, View v, int position, long id) { 
    super.onListItemClick(l, v, position, id); 
    Intent intent = new Intent(getActivity(), ListRestDetailActivity.class); 
    intent.putExtra(ListRestDetailActivity.EXTRA_RESTAURANT,mAdapter.getItem(position)); 

    startActivity(intent); 
} 

}

餐廳列表:

<string name="restaurant_list">https://gist.githubusercontent.com/jayGorio/23fed06b792dd59ccc60/raw/ba7e3943c01703f032692b363088be519e022399</string> 
+5

*當我改變爲getItem(位置).getThumbnail(),以 「HTTP://some_image_here.png」 我可以看到圖片*然後'getItem(position).getThumbnail()'返回一個無效的url – Blackbelt

+0

@Arthur Korchagin感謝您的編輯。你能爲我的問題提供一些解決方案嗎? –

+0

正如Blackbelt上面所說的,getThumbnail正在返回一個無效的URL,您應該調試並找出getThumbnail返回的內容。 – Cata

回答

-1

只使用Log.d( 「的ImageUrl」 的getItem(位置).getThumbnail()。 toString())並查看要獲取的日誌如果它返回有效或無效的URL。

:d

+0

它不顯示url是否有效。但是當我點擊頁面時它會返回錯誤。 –

0

嘗試:

Picasso.with(getContext()) 
     .load(new File(getItem(position).getThumbnail())) 
     .into(holder.thumbnail); 
+0

我得到這個錯誤:12-02 22:57:04.334 16665-16665/com.findmerest E/AndroidRuntime:致命例外:main java.lang.NullPointerException at java.io.File.fixSlashes(File.java:185 ) 在java.io.File。在android.widget.ListView.obtainView(AbsListView.java:2159) at com.findmerest.adapter.ListAdapter.getView(ListAdapter.java:44) (012)。 makeAndAddView(ListView控件。java:1831) –

+0

嘗試打印它爲''Log.e(「ImageUrl」,getItem(position).getThumbnail());'並以紅色查看日誌什麼是ImageUrl打印的價值。 –

+0

問題是,你得到空。檢查您收到的數據。 –