在Android中显示来自网址的图片?

时间:2014-03-22 13:03:02

标签: android android-listview android-imageview android-arrayadapter android-custom-view

如何将Image URL转换为任何可显示的类型,我想用我的CustomAdapter将其显示为" Category_Name"和" Category_Description"如下面的代码所示;

JSONArray jsonResponse = new JSONArray(result); // Result is my JSON
                asd = new String[3][jsonResponse.length()];
                rowItems = new ArrayList<RowItem>();
                for (int i = 0; i < jsonResponse.length(); i++) {
                    JSONObject js = jsonResponse.getJSONObject(i);
asd[0][i]= js.getString("Category_Name"); // Fetch Category Name
asd[1][i]= js.getString("Category_Description"); // Fetch Category Description
asd[2][i]= js.getString("Image"); // Fetch Image URL

RowItem item = new RowItem(R.drawable.abc_ab_bottom_solid_dark_holo, asd[0][i], asd[1][i]); 
// I delivered asd[0][i] as a title and asd[1][0] as a post content..
rowItems.add(item);
                }
adapter = new CustomListViewAdapter(MainActivity.this,R.layout.list_item, rowItems);
                    listView.setAdapter(adapter);

如何使用提取的图片网址并将其显示为标题,发布内容。

自定义适配器;

private class ViewHolder {
        ImageView imageView;
        TextView txtTitle;
        TextView txtDesc;
    }
public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        RowItem rowItem = getItem(position);

        LayoutInflater mInflater = (LayoutInflater) context
                .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.list_item, null);
            holder = new ViewHolder();
            holder.txtDesc = (TextView) convertView.findViewById(R.id.desc);
            holder.txtTitle = (TextView) convertView.findViewById(R.id.title);
            holder.imageView = (ImageView) convertView.findViewById(R.id.icon);
            convertView.setTag(holder);
        } else
            holder = (ViewHolder) convertView.getTag();

        holder.txtDesc.setText(rowItem.getDesc()); // RowItem Setter Getter Method
        holder.txtTitle.setText(rowItem.getTitle()); 
        holder.imageView.setImageResource(rowItem.getImageId());

        return convertView;
    }
}

2 个答案:

答案 0 :(得分:0)

您必须下载图像并将其设置为imageView

下载并设置图片:

public void downloadImageFromUrl(ImageView iv, String path){
    InputStream in =null;
        Bitmap bmp=null;
        int responseCode = -1;
        try{
        URL url = new URL(path);
                HttpURLConnection con = (HttpURLConnection)url.openConnection();
                con.setDoInput(true);
                con.connect();
                responseCode = con.getResponseCode();
                if(responseCode == HttpURLConnection.HTTP_OK)
                {
                    //download the image
                    in = con.getInputStream();
                    bmp = BitmapFactory.decodeStream(in);
                    in.close();
                    if(bmp!=null)
                           iv.setImageBitmap(bmp);
                }

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

答案 1 :(得分:0)

尝试this。它在解析图像和制作自定义适配器方面给了我很多帮助。希望它也可以帮到你。