使用simpleAdapter ANDROID列表视图中显示图像 [英] Display image in listview using simpleAdapter ANDROID

查看:144
本文介绍了使用simpleAdapter ANDROID列表视图中显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在上显示图像插入使用SimpleAdapter我的列表视图Android应用程序。我的形象是从我的在线服务器检索。之后,我检索图像,我试图显示图像但无任何显示没有错误了。

下面是我的code

 类ListApplicant扩展的AsyncTask<字符串,字符串,字符串> {    //开始之前,后台线程显示进度对话框
    @覆盖
    在preExecute保护无效(){
        super.on preExecute();
        pDialog =新ProgressDialog(SignUpApplicantActivity.this);
        pDialog.setIndeterminate(假);
        pDialog.setCancelable(假);
    }    //获取所有的报名网址
    保护字符串doInBackground(字符串参数... args){
        //大厦参数
        清单<&的NameValuePair GT; PARAMS =新的ArrayList<&的NameValuePair GT;();
        params.add(新BasicNameValuePair(开斋节,EID));        // URL从获取JSON字符串
        JSONObject的JSON = jParser.makeHtt prequest(url_list_applicant,
                GET,则params);        //检查JSON响应您的日志猫
        Log.d(所有申请人:json.toString());        尝试{
            //检查成功TAG
            INT成功= json.getInt(TAG_SUCCESS);            如果(成功== 1){
                发现申请人//
                //获取申请人的数组
                申请人= json.getJSONArray(TAG_APPLICANTS);                //通过所有申请人循环
                的for(int i = 0; I< applicant.length();我++){
                    JSONObject的C = applicant.getJSONObject(I)                    //存储在变量中的每个JSON项目
                    字符串的uid = c.getString(TAG_UID);
                    字符串名称= c.getString(TAG_NAME);
                    字符串整体= c.getString(TAG_OVERALL);
                    字符串apply_datetime = c.getString(TAG_APPLY_DATETIME);                    //创建新的HashMap
                    // HashMap的<字符串,字符串>地图=新的HashMap<弦乐,
                    //字符串>();                    // IMAGE HASHMAP
                    HashMap的<字符串,字符串>地图=新的HashMap<字符串,字符串>();                    //将每个子节点的HashMap键(值)
                    map.put(TAG_UID,UID);
                    map.put(TAG_NAME,名);
                    map.put(TAG_OVERALL,整体);
                    map.put(TAG_APPLY_DATETIME,apply_datetime);                    //添加HashList到ArrayList的
                    // applicantsList.add(图)                    //挂牌,形象TO LISTVIEW
                    尝试{
                        IMAGEURL = c.getString(TAG_PHOTO);                        InputStream为=(InputStream的新的网址
                                http://ec2-175-41-164-218.ap-southeast-1.compute.amazonaws.com/android/images/
                                        + IMAGEURL).getContent();
                        D = Drawable.createFromStream(是,SRC名);
                    }赶上(例外五){
                        e.printStackTrace();
                    }                    map.put(TAG_PHOTO,d.toString());                    //添加HashList到ArrayList的
                    applicantsList.add(地图);
                }
            }
        }赶上(JSONException E){
            e.printStackTrace();
        }        返回null;
    }    //后台完成任务后辞退进度对话框
    保护无效onPostExecute(字符串FILE_URL){
        //让所有申请后关闭该对话框
        pDialog.dismiss();
        //从后台线程更新界面
        runOnUiThread(新的Runnable(){
            公共无效的run(){
                如果(applicantsList.isEmpty()){
                    applicantDisplay
                            .setText(无申请人已签署了没有?);
                }其他{                    //更新解析的JSON数据到ListView控件                    适配器=新SimpleAdapter(
                            SignUpApplicantActivity.this,applicantsList,
                            R.layout.list_applicant,新的String [] {
                                    TAG_UID,TAG_NAME,TAG_OVERALL,
                                    TAG_APPLY_DATETIME,TAG_PHOTO},
                            新的INT [] {R.id.applicantUid,
                                    R.id.applicantName,
                                    R.id.applicantOverall,
                                    R.id.apply_datetime,R.id.list_image});
                    //更新的ListView
                    setListAdapter(适配器);
                }
            }
        });
    }
}


解决方案

该问题是在这里:

  map.put(TAG_PHOTO,d.toString());

您设置键为一个字符串如可绘制@为0x12345678,然后将其绑定到 R.id.list_image 我以为是一个ImageView的。这是行不通的。

我没有用一个可绘制很喜欢,但我曾与一个位图的成功:

 位图位= BitmapFactory.de codeStream(是);

然后我推翻 bindView 我的适配器设置图像:

 公共无效bindView(查看视图,上下文的背景下,光标光标){
    //做默认的东西
    super.bindView(查看,背景,光标);    //现在设置图像
    ImageView的imgView =(ImageView的)view.findViewById(R.id.imageView1);
    imgView.setImageBitmap(位图);
}

A SimpleAdapter 不具有 bindView 方法,这样反而可以提供 ViewBinder

  mySimpleAdapter.setViewBinder(新SimpleAdapter.ViewBinder(){
    @覆盖
    公共布尔setViewValue(查看视图,对象数据,字符串textRe presentation){
        如果(view.getId()。等于(R.id.my_img_view_id)){
            ((ImageView的)视图).setImageBitmap(位图);            //我们已成功绑定这个观点
            返回true;
        }其他{
            //允许默认绑定发生
            返回false;
        }
    }
});

I am currently working on an android application displaying image into my listview using SimpleAdapter. My image is retrieved from my online server. After i retrieve the image, i tried to display the image but nothing was displayed and no error too.

Below is my code

class ListApplicant extends AsyncTask<String, String, String> {

    // Before starting background thread Show Progress Dialog
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(SignUpApplicantActivity.this);
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
    }

    // getting All applicants from URL
    protected String doInBackground(String... args) {
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("eid", eid));

        // getting JSON string from URL
        JSONObject json = jParser.makeHttpRequest(url_list_applicant,
                "GET", params);

        // Check your log cat for JSON response
        Log.d("All applicants: ", json.toString());

        try {
            // Checking for SUCCESS TAG
            int success = json.getInt(TAG_SUCCESS);

            if (success == 1) {
                // applicants found
                // Getting Array of applicants
                applicant = json.getJSONArray(TAG_APPLICANTS);

                // looping through All applicants
                for (int i = 0; i < applicant.length(); i++) {
                    JSONObject c = applicant.getJSONObject(i);

                    // Storing each JSON item in variable
                    String uid = c.getString(TAG_UID);
                    String name = c.getString(TAG_NAME);
                    String overall = c.getString(TAG_OVERALL);
                    String apply_datetime = c.getString(TAG_APPLY_DATETIME);

                    // creating new HashMap
                    // HashMap<String, String> map = new HashMap<String,
                    // String>();

                    // IMAGE HASHMAP
                    HashMap<String, String> map = new HashMap<String, String>();

                    // adding each child node to HashMap key (value)
                    map.put(TAG_UID, uid);
                    map.put(TAG_NAME, name);
                    map.put(TAG_OVERALL, overall);
                    map.put(TAG_APPLY_DATETIME, apply_datetime);

                    // adding HashList to ArrayList
                    // applicantsList.add(map);

                    // LISTING IMAGE TO LISTVIEW
                    try {
                        imageURL = c.getString(TAG_PHOTO);

                        InputStream is = (InputStream) new URL(
                                "http://ec2-175-41-164-218.ap-southeast-1.compute.amazonaws.com/android/images/"
                                        + imageURL).getContent();
                        d = Drawable.createFromStream(is, "src name");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    map.put(TAG_PHOTO, d.toString());

                    // adding HashList to ArrayList
                    applicantsList.add(map);
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }

    // After completing background task Dismiss the progress dialog
    protected void onPostExecute(String file_url) {
        // dismiss the dialog after getting all applicants
        pDialog.dismiss();
        // updating UI from Background Thread
        runOnUiThread(new Runnable() {
            public void run() {
                if (applicantsList.isEmpty()) {
                    applicantDisplay
                            .setText("No applicants have signed up yet");
                } else {

                    //Updating parsed JSON data into 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 });
                    // updating listView
                    setListAdapter(adapter);
                }
            }
        });
    }
}

解决方案

The problem is here:

map.put(TAG_PHOTO, d.toString());

You set that key to a string like "Drawable@0x12345678" and then bind it to R.id.list_image which I assume is an ImageView. That won't work.

I haven't used a Drawable quite like that, but I have had success with a Bitmap:

Bitmap bitmap = BitmapFactory.decodeStream(is);

And then I overrode bindView on my Adapter to set the image:

public void bindView(View view, Context context, Cursor cursor) {
    // do the default stuff
    super.bindView(view, context, cursor);

    // now set the image
    ImageView imgView = (ImageView) view.findViewById(R.id.imageView1);
    imgView.setImageBitmap(bitmap);
}

A SimpleAdapter doesn't have a bindView method, so instead you can provide a ViewBinder:

mySimpleAdapter.setViewBinder(new SimpleAdapter.ViewBinder() {
    @Override
    public boolean setViewValue(View view, Object data, String textRepresentation) {
        if (view.getId().equals(R.id.my_img_view_id)) {
            ((ImageView) view).setImageBitmap(bitmap);

            // we have successfully bound this view
            return true;
        } else {
            // allow default binding to occur
            return false;
        }
    }
});

这篇关于使用simpleAdapter ANDROID列表视图中显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆