天天看点

为什么我的listview中不显示图片

package com.example.hap;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import android.app.Activity;

import android.app.AlertDialog;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.ImageButton;

import android.widget.ListView;

public class ListActivityImpl extends Activity {

 private ListView listView;

 private ImageButton imgbt_sum;

 private ListViewAdapter listViewAdapter;

 private List<Map<String, Object>> listItems;

 // 为我微博好友图像,只是测试图片用。

 private String[] imgeIDs = {

   "http://42.96.135.54:8008/UpFiles/images/ad4.jpg",

   "http://42.96.135.54:8008/UpFiles/images/ad4.jpg",

   "http://42.96.135.54:8008/UpFiles/images/ad1.jpg",

   "http://42.96.135.54:8008/UpFiles/images/ad4.jpg",

   "http://42.96.135.54:8008/UpFiles/images/ad3.jpg",

   "http://42.96.135.54:8008/UpFiles/images/ad1.jpg" };

 @Override

 public void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

  setContentView(R.layout.activity_main);

  listView = (ListView) findViewById(R.id.list_goods);

  listItems = getListItems();

  listViewAdapter = new ListViewAdapter(this, listItems); // 创建适配器

  listView.setAdapter(listViewAdapter);

 }

 private List<Map<String, Object>> getListItems() {

  List<Map<String, Object>> listItems = new ArrayList<Map<String, Object>>();

  for (int i = 0; i < imgeIDs.length; i++) {

   Map<String, Object> map = new HashMap<String, Object>();

   map.put("image", imgeIDs[i]); // 图片资源

   listItems.add(map);

  }

  return listItems;

 }

}

、、、、、、、、、、、、、、、、、、、、

package com.example.hap;

import java.net.HttpURLConnection;

import java.net.URL;

import java.util.List;

import java.util.Map;

import android.app.AlertDialog;

import android.content.Context;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.graphics.drawable.BitmapDrawable;

import android.os.AsyncTask;

import android.util.Log;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.BaseAdapter;

import android.widget.Button;

import android.widget.CheckBox;

import android.widget.CompoundButton;

import android.widget.ImageView;

import android.widget.TextView;

public class ListViewAdapter extends BaseAdapter {

 private Context context; // 运行上下文

 private List<Map<String, Object>> listItems; // 商品信息集合

 private LayoutInflater listContainer; // 视图容器

 public final class ListItemView { // 自定义控件集合

  public ImageView image;

 }

 public ListViewAdapter(Context context, List<Map<String, Object>> listItems) {

  this.context = context;

  listContainer = LayoutInflater.from(context); // 创建视图容器并设置上下文

  this.listItems = listItems;

 }

 public int getCount() {

  // TODO Auto-generated method stub

  return listItems.size();

 }

 public Object getItem(int arg0) {

  // TODO Auto-generated method stub

  return null;

 }

 public long getItemId(int arg0) {

  // TODO Auto-generated method stub

  return 0;

 }

 public View getView(int position, View convertView, ViewGroup parent) {

  // TODO Auto-generated method stub

  Log.e("method", "getView");

  final int selectID = position;

  // 自定义视图

  ListItemView listItemView = null;

  if (convertView == null) {

   listItemView = new ListItemView();

   // 获取list_item布局文件的视图

   convertView = listContainer.inflate(R.layout.list_item, null);

   // 获取控件对象

   listItemView.image = (ImageView) convertView

     .findViewById(R.id.imageItem);

   // 设置控件集到convertView

   convertView.setTag(listItemView);

  } else {

   listItemView = (ListItemView) convertView.getTag();

  }

  // 设置文字和图片

  BitmapDrawable icon = null;

  try {

   String headurl = (String) listItems.get(position).get("image");

//   URL url = new URL(headurl);

//   HttpURLConnection hc = (HttpURLConnection) url.openConnection();

//   icon = new BitmapDrawable(hc.getInputStream());

//   hc.disconnect();

   URL url = new URL(headurl);

   HttpURLConnection hc = (HttpURLConnection)url.openConnection();

   icon = new BitmapDrawable(hc.getInputStream());

   hc.disconnect();

   listItemView.image.setImageDrawable(icon);

  } catch (Exception e) {

  }

//  listItemView.image.setImageDrawable(icon);

  // 注册按钮点击时间爱你

  // 注册多选框状态事件处理

  return convertView;

 }

}

class CanvasImageTask extends AsyncTask {

 private ImageView view;

 protected Bitmap doInBackground(ImageView... views) {

  this.view = views[0];

  // 根据iconUrl获取图片并渲染,iconUrl的url放在了view的tag中。

  if (this.view.getTag() != null) {

   try {

    //download url

    }

    catch (Exception e) {

     return null;

     }

    } return null;

   }

  protected void onPostExecute(Bitmap bm) {

   if (bm != null) {

    this.view.setImageBitmap(bm);

    }

   }

  @Override

  protected Object doInBackground(Object... arg0) {

   // TODO Auto-generated method stub

   return null;

  }

 }