Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/195.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android动态显示图像_Android_Image_Parsing - Fatal编程技术网

Android动态显示图像

Android动态显示图像,android,image,parsing,Android,Image,Parsing,在使用适配器将图像正确显示到imageView中时,我遇到了很多问题。所以基本上我有一个JSON字符串,我正在解析它以获得每个图像的url。然后我想以列表视图的方式显示每个图像及其标题 我在网上找到了这段代码,当只需要显示一个图像时,它可以完美地工作,但当我必须动态地执行此操作时,它就不起作用了。有人对如何让它工作有一些好的建议吗?我附上了我的部分代码作为参考 谢谢你 //results => JSON string ArrayList<HashMap<String, Ob

在使用适配器将图像正确显示到imageView中时,我遇到了很多问题。所以基本上我有一个JSON字符串,我正在解析它以获得每个图像的url。然后我想以列表视图的方式显示每个图像及其标题

我在网上找到了这段代码,当只需要显示一个图像时,它可以完美地工作,但当我必须动态地执行此操作时,它就不起作用了。有人对如何让它工作有一些好的建议吗?我附上了我的部分代码作为参考

谢谢你

 //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);
//结果=>JSON字符串
ArrayList resultList=新建ArrayList();
对于(int i=0;ivalue
地图放置(标签、封面、封面);
地图放置(标签标题、标题);
//将哈希列表添加到ArrayList
结果列表添加(地图);
}
}
}捕获(JSONException e){
e、 printStackTrace();
} 
/**
*将解析的JSON数据更新到ListView中
* */
ListAdapter=new SimpleAdapter(此,结果列表,
R.layout.list_项目,
新字符串[]{TAG_TITLE},新int[]{
R.id.title});
setListAdapter(适配器);
这是我找到的图像下载代码:

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

    }       
}
私有类MyDownloadTask扩展了AsyncTask{
@凌驾
受保护位图doInBackground(URL…参数){
URL=参数[0];
位图=空;
试一试{
URLConnection=url.openConnection();
connection.connect();
InputStream is=connection.getInputStream();
BufferedInputStream bis=新的BufferedInputStream(is);
位图=BitmapFactory.decodeStream(bis);
二、关闭();
//is.close();这是一条虚线
}捕获(例外e){
e、 printStackTrace();
返回null;
}
返回位图;
}
受保护的void onPostExecute(位图){
if(位图!=null){
ImageView myImage=(ImageView)findViewById(R.id.list\u image);
设置图像位图(位图);
}否则{
Toast.makeText(getApplicationContext(),“未能下载图像”,Toast.LENGTH_LONG.show();
}
}       
}

有相当好的库调用
AQuery
。您可以使用它,只需编写2行代码即可获得所有这些内容,此外,它还可以为您缓存所有图像,特别是在
ListView

AQuery aq = new AQuery(activity);
aq.id(R.id.image).image(url, false, true);
另外,我认为最好使用
CustomAdapter
。我不确定是否可以使用
simpledapter
处理这种情况。
希望这能对你有所帮助。

AQuery太棒了。将它集成到我的库中。