ANDROID - 如何将位图设置为simpleadapter(CLOSED)

时间:2012-11-22 03:27:49

标签: android adapter simpleadapter

我正在处理我的Android应用程序。我一直在研究和尝试各种方法,如何从我的服务器网址检索图像,并使用listview将其显示在SimpleAdapter

下载图片代码

public Bitmap download(final String image) 
{
    URL myFileUrl = null;
    try {
        myFileUrl = new URL("http://SERVER URL/images/"+image);
        HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();
        conn.setDoInput(true);
        conn.connect();
        //int length = conn.getContentLength();
        InputStream is = conn.getInputStream();
        bmImg = BitmapFactory.decodeStream(is);
    } catch (MalformedURLException e) {
        // imageLoadedHandler.sendEmptyMessage(FAILED);
    } catch (IOException e) {
        // imageLoadedHandler.sendEmptyMessage(FAILED);
    }
    return bmImg;
}

将下载的图片下载到HASHMAP

// IMAGE HASHMAP
HashMap<String, Object> map = new HashMap<String, Object>();
map.put(TAG_PHOTO, download(c.getString(TAG_PHOTO)));
applicantsList.add(map);

将已解析的JSON数据更新为ListView

adapter = new SimpleAdapter(
                            SignUpApplicantActivity.this, applicantsList,
                            R.layout.list_applicant, new String[] {
                                    TAG_UID, TAG_NAME, TAG_OVERALL,
                                    TAG_APPLY_DATETIME, TAG_PHOTO},
                            new int[] { R.id.applicantUid,
                                    R.id.applicantName,
                                    R.id.applicantOverall,
                                    R.id.apply_datetime, R.id.list_image});

                    adapter.setViewBinder(new SimpleAdapter.ViewBinder() {
                        @Override
                        public boolean setViewValue(View view, Object data,String textRepresentation) 
                        {
                            if((view instanceof ImageView) & (data instanceof Bitmap)) 
                            {
                                ImageView iv = (ImageView) view;
                                Bitmap bm = (Bitmap) data;
                                iv.setImageBitmap(bm);
                                return true;
                            }
                            return false;
                        }
                    });

                    // updating listView
                    setListAdapter(adapter);

尝试上述方法后(我认为这是我研究中最接近的答案),图像仍未在我的应用程序中显示。我需要一些帮助!提前谢谢!

0 个答案:

没有答案