【问题标题】:Display image in listview using simpleAdapter ANDROID使用 simpleAdapter ANDROID 在列表视图中显示图像
【发布时间】:2012-11-21 04:14:23
【问题描述】:

我目前正在开发一个使用 SimpleAdapter 将图像显示到我的列表视图中的 android 应用程序。我的图像是从我的在线服务器检索的。检索图像后,我尝试显示图像,但没有显示任何内容,也没有错误。

下面是我的代码

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);
                }
            }
        });
    }
}

【问题讨论】:

  • 尝试先保存图像,而不是直接从 InputStream 创建一个可绘制对象,看看是否有什么不同
  • 我还可以在哪里保存图像?
  • 没有错误,因为您在 try...catch 块中包装了很多代码。可能没有图像,因为一开始就没有 JSON。您如何使用 httpGet 获取响应字符串,然后尝试从中构建 JSONObject。在任何情况下 1)检查有效 JSON 格式的响应 2)检查解析的响应是否缺少 JSON 参数。编辑:3)并使用调试模式
  • 如果你得到一两张图片,你可能会使用 sdcard,否则使用延迟加载的概念

标签: java android listview simpleadapter


【解决方案1】:

问题出在这里:

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

您将该键设置为"Drawable@0x12345678" 之类的字符串,然后将其绑定到R.id.list_image,我假设它是一个ImageView。那是行不通的。

我还没有使用过类似的 Drawable,但我使用 Bitmap 取得了成功:

Bitmap bitmap = BitmapFactory.decodeStream(is);

然后我在我的适配器上覆盖 bindView 来设置图像:

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);
}

SimpleAdapter 没有bindView 方法,因此您可以提供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;
        }
    }
});

【讨论】:

  • 哦。对不起。我是 android 新手,我在这个问题上停留了好几个星期。对于您的建议,我会尝试一下,看看是否有帮助。谢谢
  • 如何将 bindView 覆盖到我的 simpleadapter?
  • 我相信这适用于SimpleCursorAdapter 而不适用于SimpleAdapter。我发现了这个link,这表明这些方法适用于SimpleCursorAdapter。
  • @SarahPhil 我认为您可以通过在方法开头添加@Override 来覆盖它,例如@Override public void bindView(View view, Context context, Cursor cursor) { }
猜你喜欢
  • 1970-01-01
  • 2014-07-22
  • 2021-09-20
  • 2023-03-29
  • 1970-01-01
  • 2013-05-18
  • 1970-01-01
相关资源
最近更新 更多