关于android的购物车功能是怎么实现的

 我来答
育知同创教育
2016-04-23 · 百度知道合伙人官方认证企业
育知同创教育
1【专注:Python+人工智能|Java大数据|HTML5培训】 2【免费提供名师直播课堂、公开课及视频教程】 3【地址:北京市昌平区三旗百汇物美大卖场2层,微信公众号:yuzhitc】
向TA提问
展开全部
主要代码如下: 是主要代码,

actvity中的代码:
public class ShoppingCartActivity extends BaseActivity {

private List<Test> data;
private ListView mListView;
private ShoppingCartAdapter adapter;

private RelativeLayout rlRefresh;
private TextView tvRefresh;
private ProgressBar barRefresh;
private LinearLayout clear;
private CheckBox checkBox_select_all;
private CheckBox checkBox_add;
private TextView integral_sum;
private int sum = 0;
private int[] sumIntegral;
private Context context;

@Override
protected void onCreate(Bundle bundle) {
// TODO Auto-generated method stub
super.onCreate(bundle);
setContentView(R.layout.activity_shopping_cart);
initView();
}

private void initView() {
context = this;
showpage = 1;
isPermitFlag = true;
data = new ArrayList<Test>();
// 测试数据
data.add(new Test("id", "color", "type", "100"));
data.add(new Test("id", "color", "type", "200"));
data.add(new Test("id", "color", "type", "300"));
adapter = new ShoppingCartAdapter(context, handler, data);
sumIntegral = new int[data.size() + 1];

checkBox_add = (CheckBox) findViewById(R.id.checkbox_add);
integral_sum = (TextView) findViewById(R.id.integral_sum);
clear = (LinearLayout) findViewById(R.id.clear);
clear.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
data.clear();
adapter.notifyDataSetChanged();
integral_sum.setText(0 + "");
checkBox_select_all.setChecked(false);
checkBox_add.setClickable(false);
}
});
cted.entrySet().iterator();
for (int i = 0; i < data.size(); i++) {
int num = data.get(i).getNum();
int integral = Integer.valueOf(data.get(i).getIntegral());
nums.add(num);
});

<CheckBox
android:layout_width="12dp"
android:layout_height="12dp"
android:background="@drawable/clear" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="清空购物车"
android:textColor="#b61d1d"
android:textSize="@dimen/small_size" />
</LinearLayout>
</LinearLayout>
<CheckBox
android:id="@+id/checkbox_add"
style="@style/CustomCheckboxTheme2"
android:layout_width="wrap_content"
android:clickable="false"
android:layout_height="wrap_content" />
Adaper中的代码:
public class ShoppingCartAdapter extends BaseAdapter {

private Context context;
private List<Test> loans;
private LayoutInflater inflater;
private static HashMap<Integer, Boolean> isSelected;
private static HashMap<Integer, Integer> numbers;
private Handler handler;
int num;// 商品数量

static class ViewHolder { // 自定义控件集合
public CheckBox ck_select;
public ImageView pic_goods;
public TextView id_goods;
public TextView color_goods;
public TextView type_goods;
public TextView integral_goods;
public AddMinusWidget add_minus;
public LinearLayout layout;
public TextView number;
public Button minus;
public Button plus;
}

/**
* 实例化Adapter
*
* @param context
* @param data
*/
public ShoppingCartAdapter(Context context, Handler handler, List<Test> data) {
this.context = context;
this.inflater = LayoutInflater.from(context);
this.loans = data;
this.handler = handler;
isSelected = new HashMap<Integer, Boolean>();
numbers = new HashMap<Integer, Integer>();
initDate();
}

private void initDate() {
for (int i = 0; i < loans.size(); i++) {
getIsSelected().put(i, false);
getNumbers().put(i, 1);
}
}

@Override
public int getCount() {
return loans.size();
}

@Override
public Object getItem(int position) {
return loans.get(position);
}

@Override
public long getItemId(int position) {
return position;
}

/**
* 计算选中商品的积分
*
* @return 返回需要付费的总积分
*/
private float getTotalPrice() {
Test bean = null;
float totalPrice = 0;
for (int i = 0; i < loans.size(); i++) {
bean = loans.get(i);
if (ShoppingCartAdapter.getIsSelected().get(i)) {
totalPrice += bean.getNum()
* Integer.valueOf(bean.getIntegral());
}
}
return totalPrice;
}

public static HashMap<Integer, Boolean> getIsSelected() {
return isSelected;
}

public static void setIsSelected(HashMap<Integer, Boolean> isSelected) {
ShoppingCartAdapter.isSelected = isSelected;
}

public static HashMap<Integer, Integer> getNumbers() {
return numbers;
}

public static void setNumbers(HashMap<Integer, Integer> numbers) {
ShoppingCartAdapter.numbers = numbers;
}

实体类
package com.autoserve.net33.model;
public class Test {
@Override
public String toString() {
return "test [id=" + id + ", color=" + color
+ ", type=" + type + ", integral=" + integral + "]";
}

public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getIntegral() {
return integral;
}
public void setIntegral(String integral) {
this.integral = integral;
}
private String id;
private String color;
private String type;
private String integral;
private int num;//商品数量
private int sumIntegral;
private boolean isChoosed; //商品是否在购物车中被选中

public Test(String id, String color, String type, String integral) {
super();
this.id = id;
this.color = color;
this.type = type;
this.integral = integral;
}
public Test() {
super();
}

public int getNum() {
return num;
}

public void setNum(int num) {
this.num = num;
}

public int getSumIntegral() {
return sumIntegral;
}

public void setSumIntegral(int sumIntegral) {
this.sumIntegral = sumIntegral;
}

public boolean isChoosed() {
return isChoosed;
}

public void setChoosed(boolean isChoosed) {
this.isChoosed = isChoosed;
}
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式