今天实现的知识点和listview+checkBox的差不多,两个一般是连在一起用的,listview+checkBox的全选,批量删除功能可以看我的另一篇博客:http://blog.youkuaiyun.com/yu_er_/article/details/51232868。如果这个看懂了,那么今天的问题就迎刃而解了。在做购物车的功能时常常会用到计算checkBox选中的总价格。
效果图:
我没选中一个都会把总价计算出来显示在最下面。
我的代码上面在重要的地方都做了注释,我就不多做解释啦。
mainActivity:
package com.example.allselect;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
MyListView listView;
static Adapter adapter;
CheckBox cb_cart_all;
static Float zongjia;
static TextView tv_cart_Allprice_1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv_cart_Allprice_1=(TextView)findViewById(R.id.tv_cart_Allprice_1);
listView=(MyListView)findViewById(R.id.listview);
cb_cart_all=(CheckBox)findViewById(R.id.cb_cart_all);
//点击全选按钮
cb_cart_all.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){
//全选
adapter.setListDataFocuseAble();
jisuanZongJia();
}else {
//取消选择
adapter.setListDataUnFocuseAble();
jisuanZongJia();
}
adapter.notifyDataSetChanged();
}
});
adapter=new Adapter(this);
listView.setAdapter