最近在做一个项目,在做购物车的时候研究适配器内的CheckBox点击后Activity的数据更新,经过网上查找资料后问题解决,当然,也得感谢一下群里的大神提供了思路。
列表中的CheckBox选中状态与一个Map进行绑定,利用 adapter.notifyDataSetChanged();来更新界面。
至于当点击item中的CheckBox时,则调用了自定义的监听器来修改数据
Activity布局代码
<?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="vertical" >
<LinearLayout
style="@style/page_bg"
android:paddingLeft="0dp"
android:paddingRight="0dp" >
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ListView
android:id="@+id/lv"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:choiceMode="multipleChoiceModal"
android:scrollbars="none" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="@dimen/y44"
android:layout_gravity="bottom"
android:background="#FF111111"
android:orientation="horizontal" >
<CheckBox
android:id="@+id/check_all"
android:layout_width="@dimen/x64"
android:layout_height="wrap_content"
android:button="@drawable/checkbox_bg"
android:text="@string/check_all"
android:textColor="#FFcccccc"
android:layout_marginLeft="@dimen/layout_tpd"
android:textSize="@dimen/flayout_font" />
<TextView
android:id="@+id/price"
android:layout_width="@dimen/x138"
android:layout_height="fill_parent"
android:gravity="center"
android:text="@string/total"
android:textColor="#FFcdcdcd"
android:textSize="@dimen/flayout_font" />
<Button
android:id="@+id/balance"
android:layout_width="@dimen/x107"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="@drawable/btn_bg"
android:text="@string/balance"
android:textColor="@color/white"
android:textSize="@dimen/flayout_font" />
</LinearLayout>
</FrameLayout>
</LinearLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#FF333333"
android:gravity="center_vertical"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="@dimen/y85"
android:orientation="horizontal"
android:paddingBottom="@dimen/layout_tpd"
android:paddingLeft="@dimen/layout_lpd"
android:paddingRight="@dimen/layout_lpd"
android:paddingTop="@dimen/layout_tpd" >
<CheckBox
android:id="@+id/cb"
style="@style/MyCheckBox"
android:layout_width="@dimen/x21"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
public class TestAdapter extends BaseAdapter {
private Context mContext;
// 填充数据的list
private ArrayList<Map<String, String>> data;
// 用来控制CheckBox的选中状况
private static HashMap<Integer, Boolean> isSelected;
private onCheckListener listener;
public TestAdapter(Context mContext, ArrayList<Map<String, String>> data) {
// TODO 自动生成的构造函数存根
this.mContext = mContext;
this.data = data;
isSelected = new HashMap<Integer, Boolean>();
for (int i = 0; i < data.size(); i++) {
getIsSelected().put(i, false);
}
}
@Override
public int getCount() {
// TODO 自动生成的方法存根
return data.size();
}
@Override
public Object getItem(int position) {
// TODO 自动生成的方法存根
return data.get(position);
}
@Override
public long getItemId(int position) {
// TODO 自动生成的方法存根
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO 自动生成的方法存根
ViewHolder holder = null;
if (convertView == null) {
// 获得ViewHolder对象
holder = new ViewHolder();
// 导入布局并赋值给convertview
convertView = LayoutInflater.from(mContext).inflate(
R.layout.adapter_test, null);
holder.cb = (CheckBox) convertView.findViewById(R.id.cb);
// 为view设置标签
convertView.setTag(holder);
} else {
// 取出holder
holder = (ViewHolder) convertView.getTag();
}
// 根据isSelected来设置checkbox的选中状况
holder.cb.setChecked(getIsSelected().get(position));
holder.cb.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO 自动生成的方法存根
if (isSelected.get(position)) {
isSelected.put(position, false);
setIsSelected(isSelected);
} else {
isSelected.put(position, true);
setIsSelected(isSelected);
}
listener.onCheck(isSelected);
}
});
return convertView;
}
public static HashMap<Integer, Boolean> getIsSelected() {
return isSelected;
}
public static void setIsSelected(HashMap<Integer, Boolean> isSelected) {
TestAdapter.isSelected = isSelected;
}
public void setListener(onCheckListener listener) {
this.listener = listener;
}
public interface onCheckListener {
void onCheck(HashMap<Integer, Boolean> map);
}
public static class ViewHolder {
CheckBox cb;
}
}
TestActivity.java
public class TestActivity extends Activity implements OnClickListener, onCheckListener {
private Button sc_balance;
private CheckBox sc_check_all;
private TextView sc_price;
private ListView sc_lv;
private TestAdapter testAdapter;
private ArrayList<Map<String, String>> data = new ArrayList<Map<String, String>>();
private int checkNum = 0; // 记录选中的条目数量
private float total = 0.00f;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO 自动生成的方法存根
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
initView();
}
private void initView() {
lv = (ListView) findViewById(R.id.lv);
check_all = (CheckBox) findViewById(R.id.check_all);
balance = (Button) findViewById(R.id.balance);
price = (TextView) findViewById(R.id.price);
Map<String, String> map = new HashMap<String, String>();
map.put("price", "666");
for(int i = 0;i < 20; i++){
data.add(map);
}
testAdapter = new TestAdapter(getApplicationContext(), data);
testAdapter.setListener(this);
lv.setAdapter(testAdapter);
check_all.setOnClickListener(this);
dataChanged();
}
@Override
public void onClick(View v) {
// TODO 自动生成的方法存根
switch (v.getId()) {
case R.id.sc_check_all:
// 全选
total = 0.00f;
if (check_all.isChecked() == true) {
// 遍历list的长度,设为已选
for (int i = 0; i < data.size(); i++) {
testAdapter.getIsSelected().put(i, true);
checkNum = data.size();
total += Float.valueOf(data.get(i).get("price")
.toString());
}
// 刷新listview和TextView的显示
dataChanged();
} else {
// 遍历list的长度,设为未选
for (int i = 0; i < data.size(); i++) {
testAdapter.getIsSelected().put(i, false);
checkNum--;// 数量减1
total = 0.00f;
}
// 刷新listview和TextView的显示
dataChanged();
}
break;
default:
break;
}
}
// 刷新listview和TextView的显示
private void dataChanged() {
DecimalFormat decimalFormat = new DecimalFormat("0.00");// 构造方法的字符格式这里如果小数不足2位,会以0补足.
String price_num = decimalFormat.format(total);// format 返回的是字符串
String str = "合计" + "¥" + price_num + " ";
// 通知listView刷新
testAdapter.notifyDataSetChanged();
// 用TextView显示
balance.setText("结算(" + checkNum + ")");
price.setText(str);
}
@Override
public void onCheck(HashMap<Integer, Boolean> map) {
// TODO 自动生成的方法存根
total = 0.00f;
checkNum = 0;
for(int i = 0 ; i < map.size(); i ++){
if (map.get(i)) {
total += Float.valueOf(data.get(i).get("price").toString());
checkNum ++;
}
}
dataChanged();
};
}