最近在做购物车这里。 在做全选的时候 总会出现getchillat()空指针的问题,所以放弃了。
后来参考了网上的一些例子。
这篇:http://www.open-open.com/lib/view/open1343744786602.html 还算可以 在适配器中把view放入map中。 算是解决了一般的问题。
至于加入之后就可以看下面
check_goods_all和delete中的代码吧
这两个函数的最后都重新设置了适配器 因为我在调用cartAdapter.notifyDataSetChanged()的时候没有变化,具体也不太清楚。但是却是好用。
因为某些原因不能提供整个项目源代码。
public class Shopping_cart_listActivity extends Activity {
private Intent intent;
private ListView listView;
private ArrayList> listItem;
// private SimpleAdapter listAdapter;
private CartAdapter cartAdapter;
private CheckBox check_goods_all;
String[] goods_name = { "奶粉", "奶粉", "奶粉", "奶粉", "奶粉", "奶粉", "奶粉", "奶粉" };
int[] goods_number = { 1, 2, 3, 4, 5, 6, 7, 8 };
double[] goods_price = { 668.00, 668.00, 668.00, 668.00, 668.00, 668.00,
668.00, 668.00 };
public void init() {
intent = new Intent();
listView = (ListView) findViewById(R.id.ListView_shopping_cart_list);
check_goods_all = (CheckBox) findViewById(R.id.check_goods_all);
check_goods_all.setChecked(false);
// listAdapter = new SimpleAdapter(this, getData(),
cartAdapter = new CartAdapter(getData(), this.getApplicationContext());
listView.setAdapter(cartAdapter);
}
public ArrayList> getData() {
listItem = new ArrayList>();
for (int i = 0; i < 8; i++) {
HashMap map = new HashMap();
map.put("goods_info", goods_name[i]);
map.put("shopping_cart_each_count", goods_number[i]);
map.put("shopping_cart_each_price", goods_price[i]);
// map.put("check_goods", false);
listItem.add(map);
}
return listItem;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shopping_cart_list);
init();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_shopping_cart_list1, menu);
return true;
}
public void check_goods_all(View v) {
/*
* for (int i = 0; i < listView.getCount(); i++) {
* cartAdapter.getIsSelected().put(i, check_goods_all.isChecked()); }
*/
for (int i = 0; i < cartAdapter.getCount(); i++) {
cartAdapter.isSelected.set(i, check_goods_all.isChecked());
}
listView.setAdapter(cartAdapter);
}
public void delete(View v) {
int num = cartAdapter.isSelected.size();
ArrayList temp_isSelected = new ArrayList();
ArrayList> temp_listItem = new ArrayList>();
int j = 0;
for (int i = 0; i < num; i++) {
if (!cartAdapter.isSelected.get(i)) {
temp_isSelected.add(j, false);
temp_listItem.add(j++, cartAdapter.list.get(i));
}
}
cartAdapter.setIsSelected(temp_isSelected);
cartAdapter.setList(temp_listItem);
listView.setAdapter(cartAdapter);
System.out.println("适配器中剩余:" + listItem.size());
/*
* for (int i = 0; i < listView.getCount(); i++) { RelativeLayout ll =
* (RelativeLayout) listView.getChildAt(i);// 获得子级 CheckBox chkone =
* (CheckBox) ll.findViewById(R.id.check_goods);// 从子级中获得控件 if
* (chkone.isChecked()) { System.out.println(i + "------->");
* listItem.remove(i); listAdapter.notifyDataSetChanged(); } }
*/
}
public void return_to_before(View v) {
intent.setClass(Shopping_cart_listActivity.this, FrameActivity.class);
startActivity(intent);
finish();
}
public void settle(View v) {
intent.setClass(Shopping_cart_listActivity.this,
BrandRankGoodsInfoActivity.class);
startActivity(intent);
finish();
}
class CartAdapter extends BaseAdapter {
// 保存listview item的状态
HashMap map = new HashMap();
// 填充数据的list
private ArrayList> list;
// 用来控制CheckBox的选中状况
private ArrayList isSelected;
// 上下文
private Context context;
// 用来导入布局
private LayoutInflater inflater = null;
// 构造器
public CartAdapter(ArrayList> list,
Context context) {
this.context = context;
this.list = list;
isSelected = new ArrayList();
// 初始化数据
initDate();
}
// 初始化isSelected的数据
private void initDate() {
for (int i = 0; i < list.size(); i++) {
isSelected.add(i, false);
}
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@SuppressWarnings("unused")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
ViewHolder holder = null;
if (map.get(position) == null) {
// 获得ViewHolder对象
// 导入布局并赋值给convertview
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = mInflater.inflate(R.layout.listview_shopping_cart, null);
holder = new ViewHolder();
holder.check_goods = (CheckBox) view
.findViewById(R.id.check_goods);
holder.shopping_cart_each_count = (EditText) view
.findViewById(R.id.shopping_cart_each_count);
holder.shopping_cart_each_price = (TextView) view
.findViewById(R.id.shopping_cart_each_price);
holder.goods_info = (TextView) view
.findViewById(R.id.goods_info);
final int p = position;
// 为view设置标签
// final boolean ischecked = holder.check_goods.isChecked();
holder.check_goods.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
CheckBox cb = (CheckBox) v;
isSelected.set(p, cb.isChecked());
/*
* holder.check_goods.toggle(); isSelected.put(position,
* holder.check_goods.isChecked());
*/
}
});
view.setTag(holder);
} else {
// 取出holder
view = map.get(position);
holder = (ViewHolder) view.getTag();
}
// 设置list中TextView的显示
holder.goods_info.setText((String) list.get(position).get(
"goods_info"));
holder.shopping_cart_each_price.setText(((Double) list
.get(position).get("shopping_cart_each_price")).toString());
holder.shopping_cart_each_count.setText(((Integer) list.get(
position).get("shopping_cart_each_count")).toString());
// 根据isSelected来设置checkbox的选中状况
holder.check_goods.setChecked(isSelected.get(position));
return view;
}
public void setList(ArrayList> list) {
this.list = list;
}
public ArrayList> getList() {
return list;
}
public ArrayList getIsSelected() {
return isSelected;
}
public void setIsSelected(ArrayList isSelected) {
this.isSelected = isSelected;
}
}
class ViewHolder {
public RelativeLayout linerlayout_shopping_cart;
public ListView ListView_shopping_cart_list;
public Button shopping_cart_reduce;
public Button shopping_cart_add;
public EditText shopping_cart_each_count;
public TextView shopping_cart_each_price;
public TextView goods_info;
public CheckBox check_goods;
}
}