Android的动态显示图像 [英] Android displaying images dynamically

查看:118
本文介绍了Android的动态显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直有很多使用适配器,以显示我的图片正确进入我的ImageView麻烦。所以基本上我有一个JSON字符串,我通过它解析得到每个图像的URL。然后我想与其在列表视图方式标题显示的每个图像。

我发现这个code网上它完美的作品只有一个图像需要被显示出来,但是当我必须动态地做到这一点是行不通的。任何人有关于如何得到它的工作的一些好的建议吗?我重视我的codeS为引用的部分。

感谢您!

  //结果=> JSON字符串
 ArrayList的<&HashMap的LT;弦乐,对象>> resultList =新的ArrayList<&HashMap的LT;弦乐,对象>>();
 的for(int i = 0; I< results.length();我++){
 JSONObject的C = results.getJSONObject(I)//存储在变量中的每个JSON项目
盖= c.getString(TAG_COVER);
字符串title = c.getString(TAG_TITLE);尝试{    网址的URL =新的URL(封面);
    新MyDownloadTask()执行(URL)的;    }赶上(MalformedURLException的E){
    // TODO自动生成catch块
    e.printStackTrace();
    }
//创建新的HashMap
HashMap的<弦乐,对象>地图=新的HashMap<弦乐,对象>();//将每个子节点的HashMap键=>值
map.put(TAG_COVER,盖);
map.put(TAG_TITLE,职称);
//添加HashList到ArrayList的
resultList.add(地图);
}
}
   }赶上(JSONException E){
e.printStackTrace();
  }/ **
 *更新解析JSON数据到ListView控件
 * * /
ListAdapter适配器=新SimpleAdapter(这一点,resultList,
        R.layout.list_item,
        新的String [] {} TAG_TITLE,新的INT [] {
        R.id.title});setListAdapter(适配器);

和这里的图像下载codeS,我发现:

 私有类MyDownloadTask扩展的AsyncTask< URL,整型,位图> {
    @覆盖
    保护位图doInBackground(网址... PARAMS){
        网址URL =参数[0];
        位图位图= NULL;
        尝试{
            URLConnection的连接= url.openConnection();
            connection.connect();
            InputStream为= connection.getInputStream();
            二BufferedInputStream为=新的BufferedInputStream(是);
            位= BitmapFactory.de codeStream(之二);
            bis.close();
            //is.close();这是虚线
        }赶上(例外五){
            e.printStackTrace();
            返回null;
        }        返回位图;
    }
    保护无效onPostExecute(位图位图){
        如果(位图!= NULL){
            ImageView的MYIMAGE =(ImageView的)findViewById(R.id.list_image);
            myImage.setImageBitmap(位图);
        }其他{
            Toast.makeText(getApplicationContext(),无法下载图像,Toast.LENGTH_LONG).show();
        }    }
}


解决方案

有是pretty好的库调用 AQuery 。你可以使用它,简单通过书面方式只有2线code的得到这一切的东西,再加上它会缓存为你所有的图像,这是非常好的especcially如果您使用的是他们在的ListView

  AQuery AQ =新AQuery(活动);
aq.id(R.id.image)在图像配(URL,假的,真正的);

此外,我认为这将是更好地使用 CustomAdapter 。我不知道这是可能使用 SimpleAdapter 来处理这种情况。
希望这会帮助你。

I've been having lots of troubles using adapter to display my images properly into my imageView. So basically I have a JSON string and I am parsing through it to get the url of each image. Then I want to display the each image with its title in a list view fashion.

I found this code online where it works perfectly when only one image needs to be displayed, but it doesn't work when I have to do this dynamically. Anyone has some good suggestions on how to get it to work? I attached parts of my codes for references.

Thank you!!!

 //results => JSON string
 ArrayList<HashMap<String, Object>> resultList = new ArrayList<HashMap<String, Object>>();
 for(int i = 0; i < results.length(); i++){
 JSONObject c = results.getJSONObject(i);

// Storing each json item in variable
cover = c.getString(TAG_COVER);
String title = c.getString(TAG_TITLE);

try {

    URL urlS = new URL(cover);
    new MyDownloadTask().execute(urlS);

    }catch (MalformedURLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
// creating new HashMap
HashMap<String, Object> map = new HashMap<String, Object>(); 

// adding each child node to HashMap key => value
map.put(TAG_COVER, cover);
map.put(TAG_TITLE, title);
// adding HashList to ArrayList
resultList.add(map);
}
}
   } catch (JSONException e) {
e.printStackTrace();
  } 

/**
 * Updating parsed JSON data into ListView
 * */
ListAdapter adapter = new SimpleAdapter(this, resultList,
        R.layout.list_item,
        new String[] {TAG_TITLE}, new int[] {
        R.id.title});

setListAdapter(adapter);

And here is the image downloaded codes I found :

private class MyDownloadTask extends AsyncTask<URL, Integer, Bitmap> {
    @Override
    protected Bitmap doInBackground(URL... params) {
        URL url = params[0];
        Bitmap bitmap = null;
        try {
            URLConnection connection = url.openConnection();
            connection.connect();
            InputStream is = connection.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(is);
            bitmap = BitmapFactory.decodeStream(bis);
            bis.close();
            //is.close(); THIS IS THE BROKEN LINE
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

        return bitmap;
    }


    protected void onPostExecute(Bitmap bitmap) {
        if (bitmap != null) {
            ImageView myImage = (ImageView) findViewById(R.id.list_image);
            myImage.setImageBitmap(bitmap);
        } else {
            Toast.makeText(getApplicationContext(), "Failed to Download Image", Toast.LENGTH_LONG).show();
        }

    }       
}

解决方案

There is pretty good library calling AQuery. YOu can use it and simple get all this stuff by writting only 2 line of code, plus it will cache all images for you and it's very good especcially if you are using them in ListView

AQuery aq = new AQuery(activity);
aq.id(R.id.image).image(url, false, true);

Also i think it will be better to use CustomAdapter. I'm not sure it's possible to handle this situation using SimpleAdapter. Hope this will help you.

这篇关于Android的动态显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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