ListView与来自URL和TextView的ImageView无法正常工作

时间:2014-11-18 17:19:01

标签: android android-listview android-imageview

我使用自定义适配器创建了一个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 个答案:

答案 0 :(得分:0)

我认为你是从url下载Image,所以我建议你使用Android Query,如果你使用它,你永远不会得到这个错误。

AQuery androidAQuery = new AQuery(this);

作为直接从网址加载图片的示例:

androidAQuery.id(ucover).image(user_pro,true,true);