简体   繁体   English

使用SimpleAdapter将获取的图像加载到listView中

[英]Load fetched Images into listView Using SimpleAdapter

I'm stuck with this problem. 我遇到了这个问题。 I fetched the image from web url(Though it is localHost) and other stuffs too. 我从web url(虽然它是localHost)和其他东西中获取了图像。 However, Other stuffs are showed up but images is not. 然而,其他东西出现了,但图像不是。 I can't figured out what's wrong. 我无法弄清楚出了什么问题。 I'm posting the code of my class. 我发布了我班级的代码。 Check it out. 看看这个。 Here is code: 这是代码:

public void updateJSONData() {

    int success;
    Bitmap photo = null;

    Intent in = getIntent();
    Bundle extras = in.getExtras();
    String city = extras.getString("CITY");
    Log.d(TAG, "City received: " + city);

    try {

        JSONParser jParser = new JSONParser();
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("CITY", city));

        JSONObject json = jParser.makeHttpRequest(FETCH_HOSTEL_URL, "POST",
                params);
        Log.d("Json response", json.toString());

        success = json.getInt(TAG_SUCCESS);
        if (success == 1) {

            mHostels = json.getJSONArray(TAG_HOSTELS);
            mHostelList = new ArrayList<HashMap<String, Object>>();

            for (int i = 0; i < mHostels.length(); i++) {
                JSONObject c = mHostels.getJSONObject(i);

                Drawable d = null;
                String hos_name = c.getString(TAG_NAME);
                String hos_rent = c.getString(TAG_RENT);
                // String hos_img = c.getString(TAG_IMG);
                String hos_imgURL = "http://192.168.2.10:80/homey_uploads/avtars/IMG_-137606592.jpg";

                try {

                    getBitmapFromUrl(hos_imgURL);
                    photo = bmp;
                    cityCap.setImageBitmap(photo);

                } catch (Exception ex) {
                    Log.d(TAG, "Bitmap Not Received");
                }

                HashMap<String, Object> map = new HashMap<String, Object>();
                map.put(TAG_NAME, hos_name);
                map.put(TAG_RENT, hos_rent);
                map.put(TAG_IMG, photo);

                mHostelList.add(map);
                Log.d(TAG, "Map Addedd!!" + mHostelList.toString());
            }
        } else if (success == 0) {
            Log.d(TAG, "Fetch Failure!!");
            done = false;
            msg = json.getString(TAG_MESSAGE);
        }

    } catch (Exception e) {
        Log.d(TAG, "Exception occured:" + e.toString());
        done = false;
    }
}

private static Bitmap getBitmapFromUrl(String hos_imgURL) throws IOException {

    URL IMGURL = new URL(hos_imgURL);
    Log.d(TAG, "Image Url:" + IMGURL.toString());
    HttpURLConnection conn = (HttpURLConnection) IMGURL.openConnection();
    conn.setDoInput(true);
    conn.connect();

    conn.setConnectTimeout(30000);
    conn.setReadTimeout(30000);

    InputStream is = conn.getInputStream();
    bmp = BitmapFactory.decodeStream(is);

    return bmp;
}

public void updateList() {

    // LazyAdapter adapter = new LazyAdapter(SearchResult.this,
    // mHostelList);

    ListAdapter adapter = new SimpleAdapter(SearchResult.this, mHostelList,
            R.layout.single_hostel, new String[] { TAG_NAME, TAG_RENT,
                    TAG_IMG }, new int[] { R.id.tv_h_name, R.id.tv_h_rent,
                    R.id.iv_h_frontPic });

    SimpleAdapter.ViewBinder viewBinder = new SimpleAdapter.ViewBinder() {

        @Override
        public boolean setViewValue(View view, Object data,
                String textRepresentation) {
            if (view.getId() == R.id.tv_h_name) {
                ((TextView) view).setText((String) data);
                return true;
            } else if (view.getId() == R.id.tv_h_rent) {
                ((TextView) view).setText((String) data);
            } else if (view.getId() == R.id.iv_h_frontPic) {
                ((ImageView) view).setImageBitmap((Bitmap) data);
            }
            return false;
        }
    };
    ((SimpleAdapter) adapter).setViewBinder(viewBinder);

    setListAdapter(adapter);

    ListView lv = getListView();
    lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

            Intent intent = new Intent(SearchResult.this,
                    HDetailTabActivity.class);
            startActivity(intent);
        }
    });

}

public class LoadHostels extends AsyncTask<Void, Void, Boolean> {

    @Override
    protected void onPreExecute() {

        super.onPreExecute();
        pDialog = new ProgressDialog(SearchResult.this);
        pDialog.setMessage("Loading Hostels");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }

    @Override
    protected Boolean doInBackground(Void... arg0) {

        updateJSONData();
        return null;
    }

    @Override
    protected void onPostExecute(Boolean result) {
        super.onPostExecute(result);

        pDialog.dismiss();
        if (done != true) {
            Toast.makeText(SearchResult.this, "Error:" + msg,
                    Toast.LENGTH_LONG).show();
        } else if (done = true) {
            updateList();
        }
    }

}

Logcat says that unable to decodeStream but when i set the image to another imageview itshowed up. Logcat说无法解码流,但是当我将图像设置为另一个图像时,它显示出来了。 Attaching Logcat: 附加Logcat:

04-12 14:38:34.857: D/TRACKING...(8616): Map Addedd!![{h_img=android.graphics.Bitmap@4149d108, h_name=M R Hostel, h_d_mrent=4500}, {h_img=android.graphics.Bitmap@41600b78, h_name=Hostel Of Achievers, h_d_mrent=3500}, {h_img=android.graphics.Bitmap@414edd18, h_name=Hostel Of Care, h_d_mrent=2500}]
04-12 14:38:34.860: D/OpenGLRenderer(8616): Flushing caches (mode 0)
04-12 14:38:34.898: E/BitmapFactory(8616): Unable to decode stream: java.io.FileNotFoundException: /android.graphics.Bitmap@4149d108: open failed: ENOENT (No such file or directory)
04-12 14:38:34.898: I/System.out(8616): resolveUri failed on bad bitmap uri: android.graphics.Bitmap@4149d108
04-12 14:38:34.914: E/BitmapFactory(8616): Unable to decode stream: java.io.FileNotFoundException: /android.graphics.Bitmap@41600b78: open failed: ENOENT (No such file or directory)
04-12 14:38:34.914: I/System.out(8616): resolveUri failed on bad bitmap uri: android.graphics.Bitmap@41600b78
04-12 14:38:34.929: E/BitmapFactory(8616): Unable to decode stream: java.io.FileNotFoundException: /android.graphics.Bitmap@414edd18: open failed: ENOENT (No such file or directory)
04-12 14:38:34.929: I/System.out(8616): resolveUri failed on bad bitmap uri: android.graphics.Bitmap@414edd18

Please Help!!! 请帮忙!!!

SimpleAdapter doesn't support setting Bitmap s to ImageView s out of the box -- only by resource id or string url. SimpleAdapter不支持将Bitmap设置为开箱即用的ImageView - 仅限资源ID或字符串url。

So, you need to extend SimpleAdapter.ViewBinder to provide your own binding logic: 因此,您需要扩展SimpleAdapter.ViewBinder以提供您自己的绑定逻辑:

SimpleAdapter.ViewBinder viewBinder = new SimpleAdapter.ViewBinder() {
    @Override
    public boolean setViewValue(View view, Object data, String textRep) {
        if (view.getId() == R.id.tv_h_name) {
            ((TextView)view).setText((String)data);
            return true;
        } else if (view.getId() == R.id.tv_h_rent) {
            // same here
        } else if (view.getId() == R.id.iv_h_frontPic) {
            ((ImageView)view).setImageBitmap((Bitmap)data);
            return true;
        }
        return false;
    }
}
((SimpleAdapter)adapter).setViewBinder(viewBinder);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM