问题描述
- 急!遇到了一个很奇怪的集合问题,在线等大神解答!!!
-
如标题那样,集合A是个全局变量,A是要用来加入集合B中去的。第一次加入没有问题,在第二次A的元素改变之后再次加入到B中去的时候,B中属于A的集合那部分属性的值全部变成A现在的值了。这是为什么呢,在线等大神解答啊。二楼上代码
解决方案
LinearLayout layout6;
LinearLayout layout7;
LinearLayout layout8;
Button mShopping_Car;
Button mShow_Order;
ArrayList<TaoCanItem> mList1 = new ArrayList<TaoCanItem>();
ArrayList<CaiItem> hun_list = new ArrayList<CaiItem>();
ArrayList<CaiItem> su_list = new ArrayList<CaiItem>();
ArrayList<String> order_hun = new ArrayList<String>();
ArrayList<String> order_su = new ArrayList<String>();
ArrayList<TaoCanOrder> order_list = new ArrayList<TaoCanOrder>();
int hun_count = 0;
int su_count = 0;
Double temp_price = 0.0;
String temp_str = "";
解决方案二:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_taocan);
layout6 = (LinearLayout) findViewById(R.id.layout6);
layout7 = (LinearLayout) findViewById(R.id.layout7);
layout8 = (LinearLayout) findViewById(R.id.layout8);
mShopping_Car = (Button) findViewById(R.id.shopping_car);
mShow_Order = (Button) findViewById(R.id.show_order);
mShow_Order.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO 利用order_list查看订的盒饭
}
});
mShopping_Car.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (order_hun.size() == 0 || order_su.size() == 0) {
Toast.makeText(TaoCanActivity.this, "请将套餐选择完整",
Toast.LENGTH_SHORT).show();
return;
}
TaoCanOrder order = new TaoCanOrder();
ArrayList<String> temp_order_hun = new ArrayList<String>();
temp_order_hun = order_hun;
ArrayList<String> temp_order_su = new ArrayList<String>();
temp_order_su = order_su;
order.name = temp_str;
order.price = temp_price;
order.hun_list = temp_order_hun;
order.su_list = temp_order_su;
order_list.add(order);
Toast.makeText(TaoCanActivity.this, "加入购物车成功",
Toast.LENGTH_SHORT).show();
}
});
TaoCanItem item = new TaoCanItem();
item.name = "两荤一素";
item.hun = 2;
item.su = 1;
item.price = 8.0;
TaoCanItem item2 = new TaoCanItem();
item2.name = "两荤两素";
item2.hun = 2;
item2.su = 2;
item2.price = 10.0;
mList1.add(item);
mList1.add(item2);
for (int i = 0; i < 10; i++) {
CaiItem c_item = new CaiItem();
c_item.name = "红烧肉";
c_item.sort = "1";
hun_list.add(c_item);
}
for (int i = 0; i < 10; i++) {
CaiItem c_item = new CaiItem();
c_item.name = "小白菜";
c_item.sort = "2";
su_list.add(c_item);
}
for (int i = 0; i < hun_list.size(); i++) {
layout7.addView(getCaiView(hun_list.get(i).name, hun_list.get(i), i));
}
for (int i = 0; i < su_list.size(); i++) {
layout8.addView(getCaiView(su_list.get(i).name, su_list.get(i), i));
}
for (int i = 0; i < mList1.size(); i++) {
layout6.addView(getTxtLabel(mList1.get(i).name, layout6, i, mList1));
}
}
public TextView getTxtLabel(String str, LinearLayout parent, int index,
ArrayList<TaoCanItem> list) {
TextView txt = new TextView(TaoCanActivity.this);
txt.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.MATCH_PARENT));
txt.setTextColor(Color.BLACK);
txt.setGravity(Gravity.CENTER);
txt.setTextSize(16f);
txt.setText(str);
txt.setPadding(0, 20, 0, 15);
txt.setOnClickListener(new ViewListener(txt, index, parent, list));
return txt;
}
public class ViewListener implements OnClickListener {
TextView view;
LinearLayout parent;
int index;
ArrayList<TaoCanItem> list;
public ViewListener(TextView view, int index, LinearLayout parent,
ArrayList<TaoCanItem> list) {
this.view = view;
this.index = index;
this.parent = parent;
this.list = list;
}
@Override
public void onClick(View v) {
int count = parent.getChildCount();
for (int i = 0; i < count; i++) {
if (i == index) {
parent.getChildAt(i).setBackgroundColor(Color.RED);
((TextView) parent.getChildAt(i)).setTextColor(Color.WHITE);
} else {
parent.getChildAt(i).setBackgroundColor(Color.TRANSPARENT);
((TextView) parent.getChildAt(i)).setTextColor(Color.BLACK);
}
}
hun_count = list.get(index).hun;
su_count = list.get(index).su;
temp_price = list.get(index).price;
temp_str = list.get(index).name;
}
}
public View getCaiView(String str, CaiItem c_item, int position) {
View view = getLayoutInflater().inflate(R.layout.item_cai, null);
TextView txt = (TextView) view.findViewById(R.id.cai_txt);
ImageView image = (ImageView) view.findViewById(R.id.cai_flag);
txt.setText(str);
view.setOnClickListener(new CaiListener(image, c_item, view, position));
return view;
}
public class CaiListener implements OnClickListener {
// TaoCanItem item;
ImageView image;
CaiItem c_item;
View view;
int position;
public CaiListener(ImageView image, CaiItem c_item, View view,
int position) {
this.image = image;
this.c_item = c_item;
this.view = view;
this.position = position;
}
@Override
public void onClick(View v) {
if (hun_count == 0 || su_count == 0) {
Toast.makeText(TaoCanActivity.this, "请选择套餐种类",
Toast.LENGTH_SHORT).show();
return;
}
int visible = image.getVisibility();
if (visible == View.VISIBLE) {
if (c_item.sort.equals("1")) {
if (order_hun.size() > 0) {
order_hun.remove(hun_list.get(position).name);
image.setVisibility(View.GONE);
}
} else if (c_item.sort.equals("2")) {
if (order_su.size() > 0) {
order_su.remove(su_list.get(position).name);
image.setVisibility(View.GONE);
}
}
} else {
if (c_item.sort.equals("1")) {
if (order_hun.size() < hun_count) {
order_hun.add(hun_list.get(position).name);
image.setVisibility(View.VISIBLE);
} else {
Toast.makeText(TaoCanActivity.this, "您的荤菜数量已达上限",
Toast.LENGTH_SHORT).show();
}
} else if (c_item.sort.equals("2")) {
if (order_su.size() < su_count) {
order_su.add(su_list.get(position).name);
image.setVisibility(View.VISIBLE);
} else {
Toast.makeText(TaoCanActivity.this, "您的素菜数量已达上限",
Toast.LENGTH_SHORT).show();
}
}
}
}
}
解决方案三:
以上就是全部代码了,order_su和order_hun就是集合A,order_list就是集合B。请大神指出小弟的代码有什么问题,在此拜谢啦,在线等!!
时间: 2024-12-11 15:11:05