来自 URL 和 TextView 的带有 ImageView 的 ListView 不起作用
Posted
技术标签:
【中文标题】来自 URL 和 TextView 的带有 ImageView 的 ListView 不起作用【英文标题】:ListView with ImageView from URL and TextView not working 【发布时间】:2015-01-15 23:40:03 【问题描述】:我用一个自定义适配器制作了一个 ListView,我想从 URL 加载一个 ImageView 以及一些硬编码文本。
但是我的代码不起作用。 它会加载包含所有文本的列表,但图像是空白的。 我已经设法加载了一个 ImageView,但是在 ListView 中使用多个时它不起作用。
我做错了什么?
我的代码:
AdItem.java
public class AdItem
private ImageView adImage;
private String adImageURL;
private String adTitle;
private String adPrice;
private String adSize;
private String adDetails;
private String adMessage;
private String adFavorite;
// getters and setters here
SearchResultFragment.java
public class SearchResultFragment extends Fragment
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_searchresult, container, false);
ArrayList image_details = getListData();
final ListView lv1 = (ListView) view.findViewById(R.id.listViewSearchResult);
lv1.setAdapter(new SearchResultAdapter(getActivity().getApplicationContext(), image_details));
return view;
private ArrayList getListData()
ArrayList results = new ArrayList();
AdItem adData = new AdItem();
adData.setAdImageURL("http://api.androidhive.info/images/sample.jpg");
adData.setAdTitle("Apartment");
adData.setAdPrice("300");
adData.setAdSize("40m");
adData.setAdDetails("This is a wonderful apartment at a reasonable price.");
results.add(adData);
adData = new AdItem();
adData.setAdImageURL("http://api.androidhive.info/images/sample.jpg");
adData.setAdTitle("Apartment");
adData.setAdPrice("400");
adData.setAdSize("40m");
adData.setAdDetails("This is a wonderful apartment at a reasonable price.");
results.add(adData);
adData = new AdItem();
adData.setAdImageURL("http://api.androidhive.info/images/sample.jpg");
adData.setAdTitle("Apartment");
adData.setAdPrice("500");
adData.setAdSize("40m");
adData.setAdDetails("This is a wonderful apartment at a reasonable price.");
results.add(adData);
return results;
SearchResultAdapter.java
public class SearchResultAdapter extends BaseAdapter
private ArrayList listData;
private LayoutInflater layoutInflater;
public SearchResultAdapter(Context context, ArrayList listData)
this.listData = listData;
layoutInflater = LayoutInflater.from(context);
@Override
public int getCount()
return listData.size();
@Override
public Object getItem(int position)
return listData.get(position);
@Override
public long getItemId(int position)
return position;
@Override
public View getView(int position, View convertView, ViewGroup parent)
ViewHolder holder;
if (convertView == null)
convertView = layoutInflater.inflate(R.layout.searchresult_single_item, null);
holder = new ViewHolder();
holder.adImage = (ImageView) convertView.findViewById(R.id.adImage);
holder.adTitle = (TextView) convertView.findViewById(R.id.adTitle);
holder.adPrice = (TextView) convertView.findViewById(R.id.adPrice);
holder.adSize = (TextView) convertView.findViewById(R.id.adSize);
holder.adDetails = (TextView) convertView.findViewById(R.id.adDetails);
holder.adMessage = (ImageView) convertView.findViewById(R.id.adMessage);
holder.adFavorite = (ImageView) convertView.findViewById(R.id.adFavorite);
convertView.setTag(holder);
else
holder = (ViewHolder) convertView.getTag();
AdItem adListItem = (AdItem) listData.get(position);
ImageLoader imgLoader = new ImageLoader(layoutInflater.getContext());
imgLoader.DisplayImage(adListItem.getAdImageURL(), holder.adImage);
holder.adTitle.setText(adListItem.getAdTitle());
holder.adPrice.setText(adListItem.getAdPrice());
holder.adSize.setText(adListItem.getAdSize());
holder.adDetails.setText(adListItem.getAdDetails());
return convertView;
static class ViewHolder
ImageView adImage;
TextView adTitle;
TextView adPrice;
TextView adSize;
TextView adDetails;
ImageView adMessage;
ImageView adFavorite;
来源:http://javatechig.com/android/android-listview-tutorial http://www.androidhive.info/2012/07/android-loading-image-from-url-http/
【问题讨论】:
【参考方案1】:我认为你是从 url 下载图片,所以我建议你使用 Android Query,如果你使用它,你将永远不会得到这个错误。
AQuery androidAQuery=new AQuery(this);
以直接从url加载图片为例:
androidAQuery.id(ucover).image(user_pro, true, true);
【讨论】:
以上是关于来自 URL 和 TextView 的带有 ImageView 的 ListView 不起作用的主要内容,如果未能解决你的问题,请参考以下文章
如何通过 asyncTask 方法解析来自 2 个不同 URL 的数据
带有ima扩展的android exoplayer无法在react native中自动显示广告的倒计时
YouTube Vimeo 视频 ID 来自嵌入代码或来自带有 PHP 正则表达式 RegEx 的 URL