android - 从 JSON URL 加载图像到 ListView

标签 android json bitmap android-listview

我正在尝试从 URL 中获取图像,然后将其添加到 ListView 中。我可以成功获取图像,但我很难将其添加到 ListView 。

我试过这种方式:

    ListView lv = (ListView) findViewById(R.id.list);

    try {

        URL url = new URL("http://devcms.barcodo.com/Images/ProductImages/ThumbnailImages100/EG-BIRT-ST-JA_th.jpg");
        HttpGet httpRequest = null;
        httpRequest = new HttpGet(url.toURI());
        HttpClient httpclient = new DefaultHttpClient();
        HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);
        HttpEntity entity = response.getEntity();
        BufferedHttpEntity b_entity = new BufferedHttpEntity(entity);
        InputStream input = b_entity.getContent();
        Bitmap bitmap = BitmapFactory.decodeStream(input); 

        HashMap<String, Object> docList = new HashMap<String, Object>();
        docList.put("IMAGE", bitmap);

        ArrayList<HashMap<String, Object>> al = new ArrayList<HashMap<String, Object>>();
        al.add(docList);

        simpleAdapter = new SimpleAdapter(this, al, R.layout.list_item,new String[] { "IMAGE" }, new int[] { R.id.image});
        lv.setAdapter(simpleAdapter);

注意:我可以将其放入 ImageView,但想获取到 ListView 。

最佳答案

尝试以下操作:

         public class ImageURL extends ListActivity{
        @Override
     public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (android.os.Build.VERSION.SDK_INT > 9) {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                .permitAll().build();
        StrictMode.setThreadPolicy(policy);
    }
    try {

        URL url = new URL(
                "http://devcms.barcodo.com/Images/ProductImages/ThumbnailImages100/EG-BIRT-ST-JA_th.jpg");
        HttpGet httpRequest = null;

        httpRequest = new HttpGet(url.toURI());

        HttpClient httpclient = new DefaultHttpClient();
        HttpResponse response = (HttpResponse) httpclient
                .execute(httpRequest);

        HttpEntity entity = response.getEntity();
        BufferedHttpEntity b_entity = new BufferedHttpEntity(entity);
        InputStream input = b_entity.getContent();

        bitmap = BitmapFactory.decodeStream(input);

    } catch (URISyntaxException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();

    } catch (MalformedURLException e) {
        Log.e("log", "bad url");
    } catch (IOException e) {
        Log.e("log", "io error");
    }
    setListAdapter(new StudentListAdapter(this));
}

private class StudentListAdapter extends BaseAdapter {
    private Context mContext;
    private String[] mStudents = { "DurgaPrasad", "Raghu", "Vivek",
            "Satish", "Naga Jyothi", "Vardhika", "Nikhil" };
    private String[] mDetailsStudent = { "Details of DurgaPrasad",
            "Details of  Raghu This row is not created using java",
            "Details of Vivek", "Details of Satish",
            "Details of Naga Jyothi", "Details of Vardhika",
            "Details of Nikhil" };

    public StudentListAdapter(Context context) {
        mContext = context;
    }

    public int getCount() {
        return mStudents.length;
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        if (v == null) {
            System.out.println("111111111111 : " + position);
            LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            /*
             * if (position == 0) {
             * System.out.println("111111111111 : "+position); v =
             * vi.inflate(R.layout.studentdetailsrow, null);
             * System.out.println("111111111111 : "+position); } else
             */
            v = vi.inflate(R.layout.list_item, null);
        }

        ImageView iv = (ImageView) v.findViewById(R.id.icon);
        ImageView iv2 = (ImageView) v.findViewById(R.id.icon2);
        if (position == 0) {
            iv.setImageBitmap(bitmap);
            // iv2.setImageResource(R.drawable.icon);
        } else {
            iv.setImageBitmap(bitmap);
            // iv2.setImageResource(R.drawable.icon);
        }

        TextView tvname = (TextView) v.findViewById(R.id.stuname);
        TextView tvdetail = (TextView) v.findViewById(R.id.studetail);
        tvname.setText(mStudents[position]);
        tvdetail.setText(mDetailsStudent[position]);
        return v;
    }

    };
        }

关于android - 从 JSON URL 加载图像到 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13698135/

相关文章:

c# - 如何使用 DataContractJsonSerializer 序列化匿名类型

Java无法解析JSON.stringify生成的JSON

android - 在Android中建立蓝牙微微网

java - 如何将 json ListView 上选定项目的值发送到另一个 Activity ?

java - 如何在android中使用JSONObject解析转义的Json

r - 如何将图像转换为ggplot2散点图的点数据

c++ - 如何在 C++(无 .NET)中将 PDF 转换为位图(栅格化)

android - Bitmap.Options.inSampleSize 应该如何工作?

android - 适用于 iOS 和 Android 的音频格式

java - 在现有数据库中创建表 sqlite sugar orm