实现:当前面购物车被checkbox勾选的价格发生变化时,自动生成下图:
但有个问题:adapter是自定义的,在另一个文件 ,它计算好总价格传到这个文件的,listview要怎么去监听呢?
QQ 1079286062,文字不好解释清楚,希望大家帮忙。
代码如下(只要实现总价会动态变化的功能就好,值allPrice我测试过了,没有错):
public class ShopCartFragment extends BaseFragment implements IShopCartView,
OnClickListener {
private LinearLayout llConfirmOrder;// 确认订单
private ShopCartPresenter mPresenter;
ListView lvShopCart;// 购物车列表
public ShopCartListAdapter mAdapter;
ShopCart shopCart;
TextView tvTotalPrice;// 总价
private ImageView ivSearch;
int allPrice;// 计算总价
public ArrayList<String> arrayList = new ArrayList<String>();
public List<ShopCart> list = new ArrayList<ShopCart>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_shopcart, container, false);
View view = LayoutInflater.from(getActivity()).inflate(
R.layout.item_shoppingcart, null);
// lvShopCart.get
mPresenter = new ShopCartPresenter(this);
llConfirmOrder = (LinearLayout) v.findViewById(R.id.confirm_order);
lvShopCart = (ListView) v.findViewById(R.id.lv_shop_cart);
tvTotalPrice = (TextView) v.findViewById(R.id.total_price);
ivSearch = (ImageView) v.findViewById(R.id.search);
if (mAdapter == null) {
mAdapter = new ShopCartListAdapter(getActivity(),
mPresenter.getShopCart(getUserId()));
lvShopCart.setAdapter(mAdapter);
} else {
mAdapter.notifyDataSetChanged();
}
// allPrice = mAdapter.allPrice;
// tvTotalPrice.setText(Double.toString(allPrice));
lvShopCart.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
allPrice = mAdapter.allPrice;
tvTotalPrice.setText(Double.toString(allPrice));
}
});
llConfirmOrder.setOnClickListener(this);
ivSearch.setVisibility(View.INVISIBLE);
// arrayList = mAdapter.flag;
return v;
}
@Override
public void onClick(View arg0) {
switch (arg0.getId()) {
case R.id.confirm_order:
Intent i = new Intent(getActivity(), ConfirmOrderActivity.class);
list = mAdapter.shopCartListChecked;
String data = JsonUtil.toJson(list);
i.putExtra(ConfirmOrderActivity.INIT_DATA, data);
i.putStringArrayListExtra(ConfirmOrderActivity.ARRAY_LIST,
arrayList);
startActivity(i);
break;
default:
break;
}
}
@Override
public String getUserId() {
return MyApplication.userId;
}
@Override
public String getShopCartId() {
return shopCart.getId();
}
}
适配器代码如下:
public class ShopCartListAdapter extends BaseObjectListAdapter<ShopCart>
implements OnClickListener, IShopCartView {
public double dAllPrice = 0.00;
Button btAddNum;// 添加数量
Button btRemoveNum;// 减少数量
public List<Integer> isSelected = new ArrayList<Integer>();
public List<ShopCart> shopCartListChecked = new ArrayList<ShopCart>();
Holder holder = null;
ShopCart shopCart = new ShopCart();
private ShopCartPresenter mPresenter = new ShopCartPresenter(this);
private Set<Integer> positionSet = new HashSet<Integer>(); // 记录复选框勾选的位置
public int allPrice = 0;// 计算总价
private static class Holder {
ImageView imgContent;
TextView tvName;
TextView tvPrice;
TextView tvNum;
CheckBox cbCheck;
}
private ImageLoader imageLoader = ImageLoader.getInstance();
public ShopCartListAdapter(Context context, List<ShopCart> shopCart) {
super(context, shopCart);
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
holder = new Holder();
convertView = mInflater.inflate(R.layout.item_shoppingcart, null);
holder.imgContent = (ImageView) convertView
.findViewById(R.id.item_img);
holder.tvName = (TextView) convertView.findViewById(R.id.item_name);
holder.tvPrice = (TextView) convertView
.findViewById(R.id.item_price);
holder.tvNum = (TextView) convertView.findViewById(R.id.item_num);
holder.cbCheck = (CheckBox) convertView
.findViewById(R.id.item_checkbox);
// viewMap.put(position, holder);
convertView.setTag(holder);
} else {
holder = (Holder) convertView.getTag();
}
final ShopCart item = getItem(position);
imageLoader.displayImage(item.getImgUrl(), holder.imgContent);
holder.tvName.setText(item.getName());
holder.tvPrice.setText(Double.toString(item.getPrice()));
holder.tvNum.setText(Integer.toString(item.getNum()));
holder.cbCheck.setId(position);
if (positionSet.contains(position)) { // 记住勾选状态
holder.cbCheck.setChecked(true);
} else {
holder.cbCheck.setChecked(false);
}
btAddNum = (Button) convertView.findViewById(R.id.item_num_add);
btRemoveNum = (Button) convertView.findViewById(R.id.item_num_remove);
btAddNum.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
changeNum(item, item.getNum() + 1);
allPrice = allPrice + item.getPrice();
}
});
btRemoveNum.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
changeNum(item, item.getNum() - 1);
if (allPrice != 0) {
allPrice = allPrice - item.getPrice();
}
}
});
holder.cbCheck.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (item.getIsCheck() == 0) {
positionSet.add(v.getId());
item.setIsCheck(1);
shopCartListChecked.add(item);
allPrice = allPrice + item.getNum() * item.getPrice();
} else if (item.getIsCheck() == 1) {
item.setIsCheck(0);
positionSet.remove(v.getId());
shopCartListChecked.remove(item);
if (allPrice != 0) {
allPrice = allPrice - item.getNum() * item.getPrice();
}
}
}
});
return convertView;
}
private void changeNum(final ShopCart item, final int num) {
mPresenter.changeShopCartNum(getUserId(), getShopCartId(), num);
item.setNum(num);
notifyDataSetChanged();
}
@Override
public void onClick(View arg0) {
}
@Override
public String getUserId() {
return MyApplication.userId;
}
@Override
public String getShopCartId() {
return shopCart.getId();
}
public Set<Integer> getPositionSet() { // 或取勾选项的下表
return positionSet;
}
}
但有个问题:adapter是自定义的,在另一个文件 ,它计算好总价格传到这个文件的,listview要怎么去监听呢?
QQ 1079286062,文字不好解释清楚,希望大家帮忙。
代码如下(只要实现总价会动态变化的功能就好,值allPrice我测试过了,没有错):
public class ShopCartFragment extends BaseFragment implements IShopCartView,
OnClickListener {
private LinearLayout llConfirmOrder;// 确认订单
private ShopCartPresenter mPresenter;
ListView lvShopCart;// 购物车列表
public ShopCartListAdapter mAdapter;
ShopCart shopCart;
TextView tvTotalPrice;// 总价
private ImageView ivSearch;
int allPrice;// 计算总价
public ArrayList<String> arrayList = new ArrayList<String>();
public List<ShopCart> list = new ArrayList<ShopCart>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_shopcart, container, false);
View view = LayoutInflater.from(getActivity()).inflate(
R.layout.item_shoppingcart, null);
// lvShopCart.get
mPresenter = new ShopCartPresenter(this);
llConfirmOrder = (LinearLayout) v.findViewById(R.id.confirm_order);
lvShopCart = (ListView) v.findViewById(R.id.lv_shop_cart);
tvTotalPrice = (TextView) v.findViewById(R.id.total_price);
ivSearch = (ImageView) v.findViewById(R.id.search);
if (mAdapter == null) {
mAdapter = new ShopCartListAdapter(getActivity(),
mPresenter.getShopCart(getUserId()));
lvShopCart.setAdapter(mAdapter);
} else {
mAdapter.notifyDataSetChanged();
}
// allPrice = mAdapter.allPrice;
// tvTotalPrice.setText(Double.toString(allPrice));
lvShopCart.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
allPrice = mAdapter.allPrice;
tvTotalPrice.setText(Double.toString(allPrice));
}
});
llConfirmOrder.setOnClickListener(this);
ivSearch.setVisibility(View.INVISIBLE);
// arrayList = mAdapter.flag;
return v;
}
@Override
public void onClick(View arg0) {
switch (arg0.getId()) {
case R.id.confirm_order:
Intent i = new Intent(getActivity(), ConfirmOrderActivity.class);
list = mAdapter.shopCartListChecked;
String data = JsonUtil.toJson(list);
i.putExtra(ConfirmOrderActivity.INIT_DATA, data);
i.putStringArrayListExtra(ConfirmOrderActivity.ARRAY_LIST,
arrayList);
startActivity(i);
break;
default:
break;
}
}
@Override
public String getUserId() {
return MyApplication.userId;
}
@Override
public String getShopCartId() {
return shopCart.getId();
}
}
适配器代码如下:
public class ShopCartListAdapter extends BaseObjectListAdapter<ShopCart>
implements OnClickListener, IShopCartView {
public double dAllPrice = 0.00;
Button btAddNum;// 添加数量
Button btRemoveNum;// 减少数量
public List<Integer> isSelected = new ArrayList<Integer>();
public List<ShopCart> shopCartListChecked = new ArrayList<ShopCart>();
Holder holder = null;
ShopCart shopCart = new ShopCart();
private ShopCartPresenter mPresenter = new ShopCartPresenter(this);
private Set<Integer> positionSet = new HashSet<Integer>(); // 记录复选框勾选的位置
public int allPrice = 0;// 计算总价
private static class Holder {
ImageView imgContent;
TextView tvName;
TextView tvPrice;
TextView tvNum;
CheckBox cbCheck;
}
private ImageLoader imageLoader = ImageLoader.getInstance();
public ShopCartListAdapter(Context context, List<ShopCart> shopCart) {
super(context, shopCart);
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
holder = new Holder();
convertView = mInflater.inflate(R.layout.item_shoppingcart, null);
holder.imgContent = (ImageView) convertView
.findViewById(R.id.item_img);
holder.tvName = (TextView) convertView.findViewById(R.id.item_name);
holder.tvPrice = (TextView) convertView
.findViewById(R.id.item_price);
holder.tvNum = (TextView) convertView.findViewById(R.id.item_num);
holder.cbCheck = (CheckBox) convertView
.findViewById(R.id.item_checkbox);
// viewMap.put(position, holder);
convertView.setTag(holder);
} else {
holder = (Holder) convertView.getTag();
}
final ShopCart item = getItem(position);
imageLoader.displayImage(item.getImgUrl(), holder.imgContent);
holder.tvName.setText(item.getName());
holder.tvPrice.setText(Double.toString(item.getPrice()));
holder.tvNum.setText(Integer.toString(item.getNum()));
holder.cbCheck.setId(position);
if (positionSet.contains(position)) { // 记住勾选状态
holder.cbCheck.setChecked(true);
} else {
holder.cbCheck.setChecked(false);
}
btAddNum = (Button) convertView.findViewById(R.id.item_num_add);
btRemoveNum = (Button) convertView.findViewById(R.id.item_num_remove);
btAddNum.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
changeNum(item, item.getNum() + 1);
allPrice = allPrice + item.getPrice();
}
});
btRemoveNum.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
changeNum(item, item.getNum() - 1);
if (allPrice != 0) {
allPrice = allPrice - item.getPrice();
}
}
});
holder.cbCheck.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (item.getIsCheck() == 0) {
positionSet.add(v.getId());
item.setIsCheck(1);
shopCartListChecked.add(item);
allPrice = allPrice + item.getNum() * item.getPrice();
} else if (item.getIsCheck() == 1) {
item.setIsCheck(0);
positionSet.remove(v.getId());
shopCartListChecked.remove(item);
if (allPrice != 0) {
allPrice = allPrice - item.getNum() * item.getPrice();
}
}
}
});
return convertView;
}
private void changeNum(final ShopCart item, final int num) {
mPresenter.changeShopCartNum(getUserId(), getShopCartId(), num);
item.setNum(num);
notifyDataSetChanged();
}
@Override
public void onClick(View arg0) {
}
@Override
public String getUserId() {
return MyApplication.userId;
}
@Override
public String getShopCartId() {
return shopCart.getId();
}
public Set<Integer> getPositionSet() { // 或取勾选项的下表
return positionSet;
}
}