使用Picasso依赖项获取图像时遇到此问题。但当我获取其他字符串时,它们会显示在我的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()更改为&#34; http://some_image_here.png&#34;我可以看到这张照片。
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>
答案 0 :(得分:0)
试一试:
Picasso.with(getContext())
.load(new File(getItem(position).getThumbnail()))
.into(holder.thumbnail);
答案 1 :(得分:-1)
只需使用Log.d(&#34; ImageUrl&#34;,getItem(position).getThumbnail()。toString())并查看Log to get如果它返回有效或无效的URL。
:d