问题突破:
1.listView条目中有checkbox,要解决checkbox复用问题。
2.要记录选中的checkbox条目数。
---------------------------activity_main--------------------------------
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="44dp" android:orientation="horizontal" > <CheckBox android:id="@+id/main_all_check" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="12sp" android:text="全选" /> <TextView android:layout_marginLeft="20dp" android:layout_width="0dp" android:layout_weight="2" android:layout_height="wrap_content" android:text="品名" /> <TextView android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:text="数量" /> <TextView android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:text="金额" /> </LinearLayout> <ListView android:id="@+id/main_listView_list" android:layout_width="match_parent" android:layout_height="0dp" android:background="#eeeeee" android:layout_weight="1" /> <LinearLayout android:layout_width="match_parent" android:layout_height="44dp" android:orientation="horizontal" > <LinearLayout android:id="@+id/clear_main" android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" android:gravity="center" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18sp" android:text="清空(" /> <TextView android:id="@+id/clear_number" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18sp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18sp" android:text=")" /> </LinearLayout> <TextView android:layout_width="1dp" android:layout_height="match_parent" android:background="#eeeeee" android:paddingTop="3dp" android:paddingBottom="3dp" /> <TextView android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" android:textSize="18sp" android:gravity="center" android:text="挂单" /> </LinearLayout> <Button android:id="@+id/add_order" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="添加订单" /> </LinearLayout> </LinearLayout>-----------------------------listview_order-----------------------------------
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:minHeight="40dp" android:layout_height="match_parent"> <CheckBox android:id="@+id/check_option" android:layout_width="wrap_content" android:layout_height="wrap_content" android:focusable="false" android:clickable="false" /> <TextView android:id="@+id/orderNumber" android:layout_marginLeft="20dp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/productName" android:layout_width="0dp" android:layout_weight="2" android:layout_height="wrap_content" /> <TextView android:id="@+id/number" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" /> <TextView android:id="@+id/money" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" /> </LinearLayout>
---------------------------MainActivity---------------------------------
package com.example.a1.d1026checkboxclear; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.AdapterView; import android.widget.Button; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.TextView; import java.util.ArrayList; import java.util.HashMap; public class MainActivity extends AppCompatActivity { private String TAG = "SPMainActivity"; public static MainActivity mainActivity; private ListView listView; private LinearLayout clear; private TextView clearNumber; private CheckBox allCheck; private Button addOrder; ArrayList<HashMap<String,String>> list=new ArrayList<HashMap<String,String>>(); public MainActivity() { this.mainActivity = this; } private OrderFromAdapter adapter; private boolean flag=true; public static int checkNum=0; private static HashMap<Integer,Boolean> map; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView = (ListView) findViewById(R.id.main_listView_list); clear = (LinearLayout) findViewById(R.id.clear_main); clearNumber = (TextView) findViewById(R.id.clear_number); allCheck = (CheckBox) findViewById(R.id.main_all_check); addOrder=(Button)findViewById(R.id.add_order); //初始化checkbox clearNumber.setText("" + checkNum); adapter = new OrderFromAdapter(list, MainActivity.this); listView.setAdapter(adapter); //选中项修改它的状态值 listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) { OrderFromAdapter.ViewHolder viewHolder = (OrderFromAdapter.ViewHolder) view.getTag(); viewHolder.checkBox.toggle(); //选中项修改它的状态值 OrderFromAdapter.getIsSelected().put(position, viewHolder.checkBox.isChecked()); // 修改清空数目 if (viewHolder.checkBox.isChecked() == true) { checkNum++; } else { checkNum--; } clearNumber.setText("" + checkNum); } }); //增加订单 addOrder.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { HashMap<String, String> map = new HashMap<String, String>(); map.put("CONTENT", "鱼香肉丝"); map.put("NUMBER", ""+1); map.put("UNITPRICE", ""+25); map.put("PRICE",""+1*25); list.add(map); OrderFromAdapter.getIsSelected().put(list.size()-1,false); dataChanged(); } }); //全选,全部消除 allCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean b) { //全选,选中个数为订单个数 if (b) { for (int i = 0; i < list.size(); i++) { OrderFromAdapter.getIsSelected().put(i, true); } checkNum = list.size(); //刷新,设置选中个数 dataChanged(); } else {//全部取消,选中个数为0 for (int i = 0; i < list.size(); i++) { OrderFromAdapter.getIsSelected().put(i, false); } checkNum = 0; //刷新,设置选中个数 dataChanged(); } } }); //清空选中项 clear.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { for (int i = list.size(); i > 0; i--) { if (OrderFromAdapter.getIsSelected().get(i-1)) { list.remove(i-1); } } //清空选中项之后,遍历集合还是多少条目,把剩下的条目相应的checkbox设置为false OrderFromAdapter.getIsSelected().clear(); for (int i = 0; i < list.size(); i++) { OrderFromAdapter.getIsSelected().put(i, false); } adapter.notifyDataSetChanged(); //清空之后清空项变为0 checkNum = 0; clearNumber.setText("" + checkNum); } }); } private void dataChanged() { // 通知listView刷新 adapter.notifyDataSetChanged(); // TextView显示最新的选中数目 clearNumber.setText(""+checkNum ); } }
----------------------OrderFromAdapter-------------------------------
package com.example.a1.d1026checkboxclear; import android.content.Context; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.CheckBox; import android.widget.TextView; import java.util.ArrayList; import java.util.HashMap; /** * Created by a1 on 2016/10/25. */ public class OrderFromAdapter extends BaseAdapter { private ArrayList<HashMap<String,String>> list; private LayoutInflater inflater; //创建一个集合,防止checkbox复用发生错乱 private static HashMap<Integer,Boolean> isSelected; private Context context; public OrderFromAdapter(ArrayList<HashMap<String,String>> list,Context context){ this.context = context; this.list=list; inflater=LayoutInflater.from(context); isSelected = new HashMap<Integer, Boolean>(); initDate(); Log.v("TAG",isSelected.toString()); } // 初始化isSelected的数据 private void initDate(){ for(int i=0; i<list.size();i++) { getIsSelected().put(i,false); } } @Override public int getCount() { return list.size(); } @Override public Object getItem(int i) { return null; } @Override public long getItemId(int i) { return 0; } @Override public View getView(final int position, View view, ViewGroup viewGroup) { if(view==null){ ViewHolder viewHolder=new ViewHolder(); view=inflater.inflate(R.layout.listview_order,null); viewHolder.checkBox=(CheckBox) view.findViewById(R.id.check_option); viewHolder.orderNumber=(TextView) view.findViewById(R.id.orderNumber); viewHolder.textView1=(TextView) view.findViewById(R.id.productName); viewHolder.textView2=(TextView) view.findViewById(R.id.number); viewHolder.textView3=(TextView) view.findViewById(R.id.money); view.setTag(viewHolder); } ViewHolder viewHolder=(ViewHolder)view.getTag(); HashMap<String, String> map =list.get(position); //点击复选框,存储相应复选框的状态 Log.v("TAG",isSelected.toString()); //因为添加订单,判断是否有数据,防止空指针异常 if(getIsSelected().size()!=0){ viewHolder.checkBox.setChecked(getIsSelected().get(position)); } if(position<9){ viewHolder.orderNumber.setText("0"+(position+1)); }else{ viewHolder.orderNumber.setText(""+(position+1)); } viewHolder.textView1.setText( map.get("CONTENT")); viewHolder.textView2.setText( map.get("NUMBER")); viewHolder.textView3.setText( map.get("PRICE")); return view; } public static HashMap<Integer,Boolean> getIsSelected() { return isSelected; } public static void setIsSelected(HashMap<Integer,Boolean> isSelected) { OrderFromAdapter.isSelected = isSelected; } public class ViewHolder{ public CheckBox checkBox; public TextView orderNumber,textView1,textView2,textView3; } }