Displaying bitmap image in imageview by simple adapter(通过简单的适配器在 imageview 中显示位图图像)
问题描述
我从一个网址获取图像.我在列表视图中使用图像视图.我想将位图图像列表添加到列表项的每一行中.我使用了 SimpleAdapter,但 imageview 显示空白.我的代码在下面!!
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 + "
");
}
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);
推荐答案
我认为是因为您正在从网络下载图像,您需要在 ASYNC 中执行这些操作,查看无痛 thrething 下载图像,然后刷新图像视图.
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.
这篇关于通过简单的适配器在 imageview 中显示位图图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:通过简单的适配器在 imageview 中显示位图图像
基础教程推荐
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01