我想從URL獲取圖像,然後將其添加到列表視圖。我可以成功獲取圖像,但我很努力將其添加到列表視圖。從JSON URL加載圖像到列表視圖
我已經試過這樣:
ListView lv = (ListView) findViewById(R.id.list);
try {
URL url = new URL("http://devcms.barcodo.com/Images/ProductImages/ThumbnailImages100/EG-BIRT-ST-JA_th.jpg");
HttpGet httpRequest = null;
httpRequest = new HttpGet(url.toURI());
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);
HttpEntity entity = response.getEntity();
BufferedHttpEntity b_entity = new BufferedHttpEntity(entity);
InputStream input = b_entity.getContent();
Bitmap bitmap = BitmapFactory.decodeStream(input);
HashMap<String, Object> docList = new HashMap<String, Object>();
docList.put("IMAGE", bitmap);
ArrayList<HashMap<String, Object>> al = new ArrayList<HashMap<String, Object>>();
al.add(docList);
simpleAdapter = new SimpleAdapter(this, al, R.layout.list_item,new String[] { "IMAGE" }, new int[] { R.id.image});
lv.setAdapter(simpleAdapter);
注:我能夠把它變成一個ImageView的,但想提取到一個列表視圖。
你得到一個錯誤?我認爲這個實現只適用於將文本應用於文本視圖。您的適配器可能認爲您的圖像是文本。您可能需要製作自定義適配器。 – mango
@mango我dint得到任何錯誤。我可以在Imageview中獲取圖像,但不能在listview中獲取圖像。 Y? –