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