android listview中checkbox(购物车,批量操作)

简介:


最近在做购物车这里。 在做全选的时候 总会出现getchillat()空指针的问题,所以放弃了。

后来参考了网上的一些例子。

这篇:http://www.open-open.com/lib/view/open1343744786602.html 还算可以  在适配器中把view放入map中。 算是解决了一般的问题。

至于加入之后就可以看下面

check_goods_all和delete中的代码吧

这两个函数的最后都重新设置了适配器 因为我在调用cartAdapter.notifyDataSetChanged()的时候没有变化,具体也不太清楚。但是却是好用。

因为某些原因不能提供整个项目源代码。

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. public class Shopping_cart_listActivity extends Activity {  
  2.   
  3.     private Intent intent;  
  4.   
  5.     private ListView listView;  
  6.     private ArrayList<HashMap<String, Object>> listItem;  
  7.     // private SimpleAdapter listAdapter;  
  8.     private CartAdapter cartAdapter;  
  9.   
  10.     private CheckBox check_goods_all;  
  11.   
  12.     String[] goods_name = { "奶粉""奶粉""奶粉""奶粉""奶粉""奶粉""奶粉""奶粉" };  
  13.     int[] goods_number = { 12345678 };  
  14.     double[] goods_price = { 668.00668.00668.00668.00668.00668.00,  
  15.             668.00668.00 };  
  16.   
  17.     public void init() {  
  18.         intent = new Intent();  
  19.         listView = (ListView) findViewById(R.id.ListView_shopping_cart_list);  
  20.   
  21.         check_goods_all = (CheckBox) findViewById(R.id.check_goods_all);  
  22.         check_goods_all.setChecked(false);  
  23.   
  24.         // listAdapter = new SimpleAdapter(this, getData(),  
  25.         cartAdapter = new CartAdapter(getData(), this.getApplicationContext());  
  26.         listView.setAdapter(cartAdapter);  
  27.   
  28.     }  
  29.   
  30.     public ArrayList<HashMap<String, Object>> getData() {  
  31.         listItem = new ArrayList<HashMap<String, Object>>();  
  32.         for (int i = 0; i < 8; i++) {  
  33.             HashMap<String, Object> map = new HashMap<String, Object>();  
  34.             map.put("goods_info", goods_name[i]);  
  35.             map.put("shopping_cart_each_count", goods_number[i]);  
  36.             map.put("shopping_cart_each_price", goods_price[i]);  
  37.             // map.put("check_goods", false);  
  38.             listItem.add(map);  
  39.         }  
  40.         return listItem;  
  41.     }  
  42.   
  43.     @Override  
  44.     protected void onCreate(Bundle savedInstanceState) {  
  45.         super.onCreate(savedInstanceState);  
  46.         setContentView(R.layout.activity_shopping_cart_list);  
  47.         init();  
  48.   
  49.     }  
  50.   
  51.     @Override  
  52.     public boolean onCreateOptionsMenu(Menu menu) {  
  53.         // Inflate the menu; this adds items to the action bar if it is present.  
  54.         getMenuInflater().inflate(R.menu.activity_shopping_cart_list1, menu);  
  55.         return true;  
  56.     }  
  57.   
  58.     public void check_goods_all(View v) {  
  59.   
  60.         /* 
  61.          * for (int i = 0; i < listView.getCount(); i++) { 
  62.          * cartAdapter.getIsSelected().put(i, check_goods_all.isChecked()); } 
  63.          */  
  64.   
  65.         for (int i = 0; i < cartAdapter.getCount(); i++) {  
  66.             cartAdapter.isSelected.set(i, check_goods_all.isChecked());  
  67.         }  
  68.         listView.setAdapter(cartAdapter);  
  69.     }  
  70.   
  71.     public void delete(View v) {  
  72.   
  73.         int num = cartAdapter.isSelected.size();  
  74.   
  75.         ArrayList<Boolean> temp_isSelected = new ArrayList<Boolean>();  
  76.         ArrayList<HashMap<String, Object>> temp_listItem = new ArrayList<HashMap<String,Object>>();  
  77.   
  78.         int j = 0;  
  79.         for (int i = 0; i < num; i++) {  
  80.             if (!cartAdapter.isSelected.get(i)) {  
  81.                 temp_isSelected.add(j, false);  
  82.                 temp_listItem.add(j++, cartAdapter.list.get(i));  
  83.             }  
  84.         }  
  85.         cartAdapter.setIsSelected(temp_isSelected);  
  86.         cartAdapter.setList(temp_listItem);  
  87.         listView.setAdapter(cartAdapter);  
  88.         System.out.println("适配器中剩余:" + listItem.size());  
  89.         /* 
  90.          * for (int i = 0; i < listView.getCount(); i++) { RelativeLayout ll = 
  91.          * (RelativeLayout) listView.getChildAt(i);// 获得子级 CheckBox chkone = 
  92.          * (CheckBox) ll.findViewById(R.id.check_goods);// 从子级中获得控件 if 
  93.          * (chkone.isChecked()) { System.out.println(i + "------->"); 
  94.          * listItem.remove(i); listAdapter.notifyDataSetChanged(); } } 
  95.          */  
  96.   
  97.     }  
  98.   
  99.     public void return_to_before(View v) {  
  100.         intent.setClass(Shopping_cart_listActivity.this, FrameActivity.class);  
  101.         startActivity(intent);  
  102.         finish();  
  103.     }  
  104.   
  105.     public void settle(View v) {  
  106.         intent.setClass(Shopping_cart_listActivity.this,  
  107.                 BrandRankGoodsInfoActivity.class);  
  108.         startActivity(intent);  
  109.         finish();  
  110.     }  
  111.   
  112.     class CartAdapter extends BaseAdapter {  
  113.   
  114.         // 保存listview item的状态  
  115.         HashMap<Integer, View> map = new HashMap<Integer, View>();  
  116.         // 填充数据的list  
  117.         private ArrayList<HashMap<String, Object>> list;  
  118.         // 用来控制CheckBox的选中状况  
  119.         private ArrayList<Boolean> isSelected;  
  120.         // 上下文  
  121.         private Context context;  
  122.         // 用来导入布局  
  123.         private LayoutInflater inflater = null;  
  124.   
  125.         // 构造器  
  126.         public CartAdapter(ArrayList<HashMap<String, Object>> list,  
  127.                 Context context) {  
  128.             this.context = context;  
  129.             this.list = list;  
  130.             isSelected = new ArrayList<Boolean>();  
  131.             // 初始化数据  
  132.             initDate();  
  133.         }  
  134.   
  135.         // 初始化isSelected的数据  
  136.         private void initDate() {  
  137.             for (int i = 0; i < list.size(); i++) {  
  138.                 isSelected.add(i, false);  
  139.             }  
  140.         }  
  141.   
  142.         @Override  
  143.         public int getCount() {  
  144.             return list.size();  
  145.         }  
  146.   
  147.         @Override  
  148.         public Object getItem(int position) {  
  149.             return list.get(position);  
  150.         }  
  151.   
  152.         @Override  
  153.         public long getItemId(int position) {  
  154.             return position;  
  155.         }  
  156.   
  157.         @SuppressWarnings("unused")  
  158.         @Override  
  159.         public View getView(int position, View convertView, ViewGroup parent) {  
  160.             View view;  
  161.             ViewHolder holder = null;  
  162.             if (map.get(position) == null) {  
  163.                 // 获得ViewHolder对象  
  164.                 // 导入布局并赋值给convertview  
  165.                 LayoutInflater mInflater = (LayoutInflater) context  
  166.                         .getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
  167.                 view = mInflater.inflate(R.layout.listview_shopping_cart, null);  
  168.                 holder = new ViewHolder();  
  169.                 holder.check_goods = (CheckBox) view  
  170.                         .findViewById(R.id.check_goods);  
  171.                 holder.shopping_cart_each_count = (EditText) view  
  172.                         .findViewById(R.id.shopping_cart_each_count);  
  173.                 holder.shopping_cart_each_price = (TextView) view  
  174.                         .findViewById(R.id.shopping_cart_each_price);  
  175.                 holder.goods_info = (TextView) view  
  176.                         .findViewById(R.id.goods_info);  
  177.                 final int p = position;  
  178.                 // 为view设置标签  
  179.                 // final boolean ischecked = holder.check_goods.isChecked();  
  180.                 holder.check_goods.setOnClickListener(new OnClickListener() {  
  181.                     @Override  
  182.                     public void onClick(View v) {  
  183.                         // TODO Auto-generated method stub  
  184.                         CheckBox cb = (CheckBox) v;  
  185.                         isSelected.set(p, cb.isChecked());  
  186.                         /* 
  187.                          * holder.check_goods.toggle(); isSelected.put(position, 
  188.                          * holder.check_goods.isChecked()); 
  189.                          */  
  190.   
  191.                     }  
  192.                 });  
  193.                 view.setTag(holder);  
  194.             } else {  
  195.                 // 取出holder  
  196.                 view = map.get(position);  
  197.                 holder = (ViewHolder) view.getTag();  
  198.             }  
  199.   
  200.             // 设置list中TextView的显示  
  201.             holder.goods_info.setText((String) list.get(position).get(  
  202.                     "goods_info"));  
  203.             holder.shopping_cart_each_price.setText(((Double) list  
  204.                     .get(position).get("shopping_cart_each_price")).toString());  
  205.             holder.shopping_cart_each_count.setText(((Integer) list.get(  
  206.                     position).get("shopping_cart_each_count")).toString());  
  207.             // 根据isSelected来设置checkbox的选中状况  
  208.             holder.check_goods.setChecked(isSelected.get(position));  
  209.             return view;  
  210.   
  211.         }  
  212.   
  213.         public void setList(ArrayList<HashMap<String, Object>> list) {  
  214.             this.list = list;  
  215.         }  
  216.           
  217.         public ArrayList<HashMap<String, Object>> getList() {  
  218.             return list;  
  219.         }  
  220.   
  221.         public ArrayList<Boolean> getIsSelected() {  
  222.             return isSelected;  
  223.         }  
  224.   
  225.         public void setIsSelected(ArrayList<Boolean> isSelected) {  
  226.             this.isSelected = isSelected;  
  227.         }  
  228.     }  
  229.   
  230.     class ViewHolder {  
  231.         public RelativeLayout linerlayout_shopping_cart;  
  232.         public ListView ListView_shopping_cart_list;  
  233.         public Button shopping_cart_reduce;  
  234.         public Button shopping_cart_add;  
  235.   
  236.         public EditText shopping_cart_each_count;  
  237.         public TextView shopping_cart_each_price;  
  238.         public TextView goods_info;  
  239.         public CheckBox check_goods;  
  240.     }  
  241.   
  242. }  

相关文章
|
3月前
|
API Android开发 开发者
Android UI设计: 什么是RecyclerView?为什么它比ListView更好?
Android UI设计: 什么是RecyclerView?为什么它比ListView更好?
31 2
|
4月前
|
XML Java Android开发
Android Studio App入门之列表视图ListView的讲解及实战(附源码 超详细必看)
Android Studio App入门之列表视图ListView的讲解及实战(附源码 超详细必看)
85 0
|
1月前
|
XML Java Android开发
[Android]CheckBox复选框
[Android]CheckBox复选框
51 0
|
4月前
|
XML Android开发 数据格式
安卓-无敌解决ListView添加标题头无法正常显示的问题(歪门邪道)
安卓-无敌解决ListView添加标题头无法正常显示的问题(歪门邪道)
37 0
|
6月前
|
存储 数据库 Android开发
Android -- 购物车
Android -- 购物车
69 0
|
6月前
|
Java Android开发
[笔记]Android 学习一之转场动画+ViewPager+ListView简单Demo
[笔记]Android 学习一之转场动画+ViewPager+ListView简单Demo
|
8月前
|
Android开发
Android 中CheckBox复选框按钮的基本用法
Android 中CheckBox复选框按钮的基本用法
93 0
Android 中CheckBox复选框按钮的基本用法
|
8月前
|
Android开发
Android 复选框CheckBox 设置只能选中其中一个
Android 复选框CheckBox 设置只能选中其中一个
178 0
|
8月前
|
XML Android开发 数据格式
Android CheckBox 复选框(自定义复选框)
Android CheckBox 复选框(自定义复选框)
239 0
|
11月前
|
XML 监控 Java
Android消息提示框及CheckBox组件
Android消息提示框及CheckBox组件
103 0