显示位图图像的ImageView通过简单的适配器位图、适配器、图像、简单

2023-09-06 00:53:04 作者:强颜≠欢笑

我从一个URL获得的图像。我使用的ImageView的列表视图。我要位图图像列表添加到列表项的每一行。我用SimpleAdapter但ImageView的显示blank.My code低于!!

 的ArrayList< HashMap的<字符串,位图>> mylist中=新的ArrayList< HashMap的<字符串,位图>>();

    束束= this.getIntent()getExtras()。
     GET = bundle.getString(姓名);

     尝试{
            HttpClient的HttpClient的=新DefaultHttpClient();
            HttpPost httppost =新HttpPost(http://www.propertyhookup.com/mobile/propertylist.php);
            namevaluepairs中=新的ArrayList<的NameValuePair>(1);
            nameValuePairs.add(新BasicNameValuePair(ZIP code,get.trim()));
            httppost.setEntity(新UrlEn codedFormEntity(namevaluepairs中));
            HTT presponse响应= httpclient.execute(httppost);
            HttpEntity实体= response.getEntity();
            是= entity.getContent();

    }赶上(例外五){
            Toast.makeText(getApplicationContext(),e.​​getMessage(),Toast.LENGTH_LONG).show();
    }

  //转换响应串
    尝试{
            的BufferedReader读卡器=新的BufferedReader(新InputStreamReader的(就是,ISO-8859-1),8);
            StringBuilder的SB =新的StringBuilder();
            串线= NULL;
            而((行= reader.readLine())!= NULL){
                    sb.append(行+\ N);
            }
            is.close();
            结果= sb.toString();
    }赶上(例外五){
            Toast.makeText(getApplicationContext(),e.​​getMessage(),Toast.LENGTH_LONG).show();
    }
    如果(result.length()&其中; = 7){
        Toast.makeText(getApplicationContext(),无属性这个zip code或检查你的拉链code,Toast.LENGTH_LONG).show();
        //text.setText("No属性这个zip code或检查你的拉链code);
    }
    其他{
    尝试{

     jArray =新的JSONObject的(结果);
    }赶上(JSONException E){
        Toast.makeText(getApplicationContext(),e.​​getMessage(),Toast.LENGTH_LONG).show();
    }

    // JSONObject的JSON = JSONfunctions.getJSONfromURL(http://192.168.1.111/propertyhookup.com/mobile/propertylist.php);

    尝试{

        JSONArray地震= jArray.getJSONArray(地震);

        的for(int i = 0;我小于10;我++){
            图=新的HashMap<字符串,位图>();
            // HashMap的<字符串,可绘制> MAP1 =新的HashMap<字符串,可绘制>();

            JSONObject的E = earthquakes.getJSONObject(我);



            如果(e.getString(房产类型)。包含(1)){
                proptype =套房;
            }否则如果(e.getString(房产类型)。包含(2)){
                proptype =公寓;
            }否则如果(e.getString(房产类型)。包含(3)){
                proptype =联排别墅;
            }
            如果(e.getString(estimated_price)。包含(0)){
                估计=没有足够的市场价值;
                // estimat =(TextView中)findViewById(R.id.estimat);
                //estimat.setTextColor(Color.rgb(0,0,23));
            }其他 {
                估计=$+ e.getString(estimated_price);
            }

            相片= e.getString(照片1);

            绘制= LoadImageFromWebOperations(照片);

            //text.setImageDrawable(d);

            尝试
            {
                    aURL =新的URL(照片);
            }
            赶上(MalformedURLException的E1)
            {
                    // TODO自动生成的catch块
                    e1.printStackTrace();
            }
            URLConnection的康恩= NULL;
            尝试
            {
                    康恩= aURL.openConnection();
            }
            赶上(IOException异常E1)
            {
                    // TODO自动生成的catch块
                    e1.printStackTrace();
            }
            尝试
            {
                    conn.connect();
            }
            赶上(IOException异常E1)
            {
                    // TODO自动生成的catch块
                    e1.printStackTrace();
            }
            InputStream的是= NULL;
            尝试
            {
                    是= conn.getInputStream();
            }
            赶上(IOException异常E1)
            {
                    // TODO自动生成的catch块
                    e1.printStackTrace();
            }
            的BufferedInputStream双=新
    的BufferedInputStream(是,8 * 1024);
            位图BM = BitmapFactory.de codeStream(之二);

            map.put(照片,BM);
            mylist.add(图)


        }
    }赶上(JSONException E){
        Toast.makeText(getApplicationContext(),e.​​getMessage(),Toast.LENGTH_LONG).show();
    }



    SimpleAdapter适配器=新SimpleAdapter(这一点,mylist中,R.layout.main4,
                   新的String [] {百分比,物业ID,城市名,国家code,属性类型,素材,卫浴,卧室,价格,估计,照片},
                   新的INT [] {R.id.percent,R.id.property_id,R.id.city_name,R.id.state_ code,R.id.prop_type,R.id.foot,R.id.bath ,R.id.bed,R.id.list,R.id.estimat,R.id.image});
    setListAdapter(适配器);
 

解决方案

我觉得是因为你下载的网站形象,你需要在异步做这些见无痛thrething下载图像和刷新后只是imageviews。

I am getting image from an url. I am using imageview in listview. I want to add the list of bitmap images into the each row of the list item. I used SimpleAdapter but the imageview shows blank.My code is below !!

 ArrayList<HashMap<String, Bitmap>> mylist = new ArrayList<HashMap<String, Bitmap>>();

    Bundle bundle = this.getIntent().getExtras();
     get = bundle.getString("name");

     try{
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://www.propertyhookup.com/mobile/propertylist.php");
            nameValuePairs = new ArrayList<NameValuePair>(1);
            nameValuePairs.add(new BasicNameValuePair("zipcode", get.trim()));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();

    }catch(Exception e){
            Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
    }

  //convert response to string
    try{
            BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
            }
            is.close();
            result=sb.toString();
    }catch(Exception e){
            Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
    }
    if(result.length()<= 7){
        Toast.makeText(getApplicationContext(), "No properties for this zipcode or check your zipcode ", Toast.LENGTH_LONG).show();
        //text.setText("No properties for this zipcode or check your zipcode");
    }
    else{
    try{

     jArray = new JSONObject(result);            
    }catch(JSONException e){
        Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
    }

    //JSONObject json = JSONfunctions.getJSONfromURL("http://192.168.1.111/propertyhookup.com/mobile/propertylist.php");

    try{

        JSONArray  earthquakes = jArray.getJSONArray("earthquakes");

        for(int i=0;i<10;i++){                      
            map = new HashMap<String, Bitmap>();
            //HashMap<String, Drawable> map1 = new HashMap<String, Drawable>();

            JSONObject e = earthquakes.getJSONObject(i);



            if(e.getString("property_type").contains("1")) {
                proptype ="Single Family Home";
            }else if(e.getString("property_type").contains("2")) {
                proptype="Condo";
            }else if(e.getString("property_type").contains("3")) {
                proptype="Townhouse";
            }
            if(e.getString("estimated_price").contains("0")) {
                estimate = "Not Enough Market Value";
                //estimat = (TextView) findViewById(R.id.estimat);
                //estimat.setTextColor(Color.rgb(0, 0, 23));
            }else {
                estimate = "$"+e.getString("estimated_price");
            }

            photo = e.getString("photo1");

            drawable = LoadImageFromWebOperations(photo);

            //text.setImageDrawable(d);

            try
            {
                    aURL = new URL(photo);
            }
            catch (MalformedURLException e1)
            {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
            }
            URLConnection conn = null;
            try
            {
                    conn = aURL.openConnection();
            }
            catch (IOException e1)
            {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
            }
            try
            {
                    conn.connect();
            }
            catch (IOException e1)
            {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
            }
            InputStream is = null;
            try
            {
                    is = conn.getInputStream();
            }
            catch (IOException e1)
            {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
            }
            BufferedInputStream bis = new
    BufferedInputStream(is,8*1024);
            Bitmap bm = BitmapFactory.decodeStream(bis);

            map.put(photos, bm);
            mylist.add(map);


        }       
    }catch(JSONException e)        {
        Toast.makeText(getApplicationContext(),e.getMessage(), Toast.LENGTH_LONG).show();
    }



    SimpleAdapter adapter = new SimpleAdapter(this, mylist , R.layout.main4, 
                   new String[] { "percent","propertyid",  "cityname", "statecode", "propertytype", "footage", "bathroom", "bedroom", "price", "estimated", "photos" }, 
                   new int[] { R.id.percent, R.id.property_id,  R.id.city_name, R.id.state_code, R.id.prop_type, R.id.foot, R.id.bath, R.id.bed, R.id.list, R.id.estimat, R.id.image});
    setListAdapter(adapter);
在PS中快速的利用位图模式将色彩图转换成黑白图

解决方案

i think is because you are downloading the image from web and you need to do these in ASYNC see painless thrething download image and after that refresh just the imageviews.