【问题标题】:Image from URL not loading in custom adapter来自 URL 的图像未加载到自定义适配器中
【发布时间】:2014-08-06 10:31:45
【问题描述】:

我正在从 Web 服务加载数据并将其显示在 ListView 中。 ListView 项目只有两个视图 - 项目描述的文本和项目图像的图像。数据被下载并传递给 AsyncTask 中的自定义适配器,该适配器在 onCreate 方法中执行。一切正常,除了 ImageViews 是空的。

为了下载图片,我使用了我在网上找到的 ImageLoader 类。它在我下载的示例中运行良好,它只需要两个参数 - 图片 url 和 imageview,所以我认为一切都在这里。

我在这里做错了什么吗?

 // Fetch latest items for home screen based on selected city
        private class LatestItems extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... loc_id) {

            // For storing data from web service
            String data = "";

            // Downloader object
            UrlDownloader downloader = new UrlDownloader();

            // Building the url to the web service
            String url = "http://www.mywebsite.com/web_services/home_screen_latest_items.php?loc_id="
                    + loc_id[0];

            try {
                // Fetching the data from web service in background
                data = downloader.downloadUrl(url);

            } catch (Exception e) {
                Log.d("Downloader exception", e.toString());
            }
            return data;
        }

        @Override
        protected void onPostExecute(String downloadedData) {
            super.onPostExecute(downloadedData);

            //If web service returned data
            if (downloadedData != null) {
                JSONObject jObject;
                List<HashMap<String, String>> items = null;

                // Instantiate parser
                ItemJSONParser placeJsonParser = new ItemJSONParser();

                try {
                    // Put data into JSON Object and pass it to parser
                    jObject = new JSONObject(downloadedData);
                    items = placeJsonParser.parse(jObject);

                } catch (Exception e) {
                    Log.d("Exception", e.toString());
                }

                // Instantiate adapter. Provide Context, Item layout and
                // DataSource (result)
                ItemPreviewAdapter itemPreview = new ItemPreviewAdapter(
                        getApplicationContext(), R.layout.lvitem_homepage_item,
                        items);

                // Pass each data to from list into to adapter
                for (HashMap<String, String> data : items) {
                    itemPreview.add(data);
                }

                // Set adapter
                latestItemsList.setAdapter(itemPreview);
            }
        }

    }

项目布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#fff"
    android:orientation="vertical"
    android:padding="5dp" >

    <ImageView
        android:id="@+id/ivItemImage"
        android:layout_width="75dp"
        android:layout_height="100dp"
        android:scaleType="centerCrop"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/tvItemName"
        android:layout_width="fill_parent"
        android:layout_height="40dp"
        android:gravity="center_horizontal"
        android:lines="2"
        android:textColor="#000"
        android:textSize="10sp" />

</LinearLayout>

自定义适配器:

public class ItemPreviewAdapter extends ArrayAdapter<HashMap<String, String>> {

    private final List<HashMap<String, String>> items;
    private final LayoutInflater mLayoutInflater;

    static class ViewHolder {
        TextView tvIname;
        ImageView image;
    }

    public ItemPreviewAdapter(final Context context,
            final int textViewResourceId, List<HashMap<String, String>> map) {
        super(context, textViewResourceId);
        mLayoutInflater = LayoutInflater.from(context);
        this.items = map;

        Log.i("Adapter", items.toString());
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder;

        if (convertView == null) {
            convertView = mLayoutInflater.inflate(
                    R.layout.lvitem_homepage_item, parent, false);

            holder = new ViewHolder();
            holder.tvIname = (TextView) convertView
                    .findViewById(R.id.tvItemName);
            holder.image = (ImageView) convertView
                    .findViewById(R.id.ivItemImage);

            convertView.setTag(holder);

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

        HashMap<String, String> item = items.get(position);

        String id = item.get("id");
        String item_name = item.get("iname");

        holder.tvIname.setText(item_name);

        String image_url = "http://www.mywebsite.com/images/items/cropped_image_list_id_"
                + id + ".png";

        ImageLoader imgLoader = new ImageLoader(getContext());
        imgLoader.DisplayImage(image_url, holder.image);

        return convertView;
    }
}

【问题讨论】:

    标签: android listview android-listview


    【解决方案1】:

    将您的图像加载器类声明为全局并在构造函数中初始化。

    ImageLoader imgLoader;
    
     public ItemPreviewAdapter(final Context context,
                final int textViewResourceId, List<HashMap<String, String>> map) {
            super(context, textViewResourceId);
            .....
            .....
    
           imgLoader = new ImageLoader(context);
        }
    

    也将它从 getView 中删除 咳咳……

    【讨论】:

    • 就是这样。是上下文问题吗?我的方式没有以正确的方式传递上下文,ImageLoader 应该接收传递给适配器构造函数的上下文?
    • 是的,实际上上下文必须是基类,所以在这里你传递 getContext() 这是适配器类上下文,还有一件事是你在获取视图中创建对象,所以在我的情况下创建了这么多对象,只有一个对象创建..
    • 是的,你是对的,我正在为每个项目分别创建 ImageLoader 实例。感谢您提供这一见解。
    【解决方案2】:

    请使用activity的上下文而不是使用Adapter类的getContext()

    Context context;
    
    
    public ItemPreviewAdapter(final Context context,final int textViewResourceId, List<HashMap<String, String>> map) 
    {
        super(context, textViewResourceId);
        mLayoutInflater = LayoutInflater.from(context);
        this.items = map;
        this.context=context;
    
        Log.i("Adapter", items.toString());
    }
    

    并使用此上下文在 getView 方法中初始化 ImageLoader 类

    ImageLoader imgLoader = new ImageLoader(context);
    

    【讨论】:

      猜你喜欢
      • 2011-07-08
      • 2015-07-09
      • 1970-01-01
      • 1970-01-01
      • 2021-11-03
      • 1970-01-01
      • 2012-03-10
      • 1970-01-01
      • 2015-09-01
      相关资源
      最近更新 更多