直接粘代码吧 - -
重写的SimpleAdapter类:
public class MySimpleAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private ArrayList<HashMap<String, Object>> list;
private int layoutID;
private String flag[];
private int ItemIDs[];
public MySimpleAdapter(Context context, ArrayList<HashMap<String, Object>> list,
int layoutID, String flag[], int ItemIDs[]) {
this.mInflater = LayoutInflater.from(context);
this.list = list;
this.layoutID = layoutID;
this.flag = flag;
this.ItemIDs = ItemIDs;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return 0;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = mInflater.inflate(layoutID, null);
convertView = mInflater.inflate(layoutID, null);
for (int i = 0; i < flag.length; i++) {
if (convertView.findViewById(ItemIDs[i]) instanceof ImageView) {
ImageView iv = (ImageView) convertView.findViewById(ItemIDs[i]);
iv.setBackgroundResource((Integer) list.get(position).get(
flag[i]));
} else if (convertView.findViewById(ItemIDs[i]) instanceof TextView) {
TextView tv = (TextView) convertView.findViewById(ItemIDs[i]);
tv.setText((String) list.get(position).get(flag[i]));
}
}
addListener(convertView);
return convertView;
}
public void addListener(View convertView) {
((TextView)convertView.findViewById(R.id.order_mine_show_handle_confirm)).setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
new AlertDialog.Builder(OrderMineNonPaymentActivity.onp)
.setTitle("自定义通用SimpleAdapter")
.setMessage("按钮成功触发监听事件!")
.show();
}
});
}
}
引用的地方 OrderMineNonPaymentActivity:
/**
* 动态生成订单列表 lipeng
*/
private void addNonPaymentList() {
order_mine_show_non_payment_list_ll = (LinearLayout) findViewById(R.id.order_mine_show_non_payment_list_ll);
nonPaymentList = (ListView) findViewById(R.id.order_mine_show_non_payment_list);
nonPaymentList.setDivider(null);
nonPaymentSimpleAdapter = new SimpleAdapter(
OrderMineNonPaymentActivity.this, goodsList,
R.layout.activity_order_non_payment_item, new String[] {
"shopname", "bigimage", "goodname" }, new int[] {
R.id.order_mine_show_shopname,
R.id.order_mine_show_goods_img,
R.id.order_mine_show_goods_name });
MySimpleAdapter nonPaymentSimpleAdapter = new MySimpleAdapter(this,
goodsList, R.layout.activity_order_mine_non_payment,
new String[] { "shopname", "bigimage", "goodname" }, new int[] {
R.id.order_mine_show_shopname,
R.id.order_mine_show_goods_img,
R.id.order_mine_show_goods_name });
nonPaymentList.setAdapter(nonPaymentSimpleAdapter);
}
private class MyOnItemClickListener implements OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
}
}
异常报的空指针,找了半天也没找到为啥空指针了
06-17 22:55:32.845: E/AndroidRuntime(1463): FATAL EXCEPTION: main
06-17 22:55:32.845: E/AndroidRuntime(1463): Process: com.zb.stlala, PID: 1463
06-17 22:55:32.845: E/AndroidRuntime(1463): java.lang.NullPointerException
06-17 22:55:32.845: E/AndroidRuntime(1463): at com.zb.stlala.adapter.MySimpleAdapter.addListener(MySimpleAdapter.java:83)
06-17 22:55:32.845: E/AndroidRuntime(1463): at com.zb.stlala.adapter.MySimpleAdapter.getView(MySimpleAdapter.java:75)