gpt4 book ai didi

java - 添加到购物车设计在 Android 中工作

转载 作者:太空宇宙 更新时间:2023-11-04 11:34:05 24 4
gpt4 key购买 nike

我正在开发一个与电子商务应用程序类似的应用程序。用户可以将产品添加到购物车。
我有两个下面提到的数组列表。

public ArrayList<Design> cartList = new ArrayList<Design>();
public ArrayList<Design> designList = new ArrayList<Design>();

一个用于显示产品列表,另一个用于保存用户添加的产品。我有两项 Activity (参见图片)

此 Activity 是在 recyclerview 中展示产品。在此我在购物车中添加了两项商品。

enter image description here

这是我的第二个 Activity ,显示所有购物车商品。 (用户也可以删除、增加或减少此 Activity 的数量。)

enter image description here

现在我想要的是,当用户返回到第一个 Activity 时,我想比较来自 cartList 的 designList 中的两个列表更新数量(个)。下面的屏幕截图是我按回或重新访问时的第一个 Activity 。

enter image description here

此图像应与第一张图像类似。我将在这里分享我的代码。

Design.java

public class Design implements Serializable {
public int id;
public String qualityId;
public String qualityName;
public String designId;
public String designName;
public String image;
public String name; //use as shade name
public String discPercent;
public String amount;
public int pcs; //consider quantity here.
}

Single_DesignAdapter.java

public class Single_DesignAdapter extends RecyclerView.Adapter<Single_DesignAdapter.ViewHolder> {

public ArrayList<Design> designList;
private Context context;
private LayoutInflater layoutInflater;
public ArrayList<Design> cartList = null;

public Single_DesignAdapter(Context context, ArrayList<Design> designList) {
this.designList = designList;
this.context = context;
this.layoutInflater = LayoutInflater.from(context);

if (cartList != null) {
String cart = getSharePref(context, "cart");
Design[] designs = new Gson().fromJson(cart, Design[].class);
cartList = (ArrayList<Design>) Arrays.asList(designs);
}
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = layoutInflater.inflate(R.layout.single_design, parent, false);
return new ViewHolder(view);
}

@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {

final Design design = designList.get(position);
holder.tvQuality.setText(design.qualityName);
holder.tvDesign.setText(design.designName);
holder.tvShade.setText(design.name);

if (!design.discPercent.equals("0")) {
holder.tvDisPer.setText("-"+design.discPercent+"%");
holder.tvAmount.setPaintFlags(holder.tvAmount.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
int a = Integer.parseInt(design.amount);
float b = Float.parseFloat(design.discPercent) / 100;
holder.tvAmount.setText("₹ " + design.amount);
int c = (int) (a * b);
int d = a-c;
holder.tvDiscAmt.setText("₹ " +d);
Picasso.with(context).load(design.image).into(holder.ivDesign);
} else {
holder.tvAmount.setText("₹ " + design.amount);
Picasso.with(context).load(design.image).into(holder.ivDesign);
holder.tvDisPer.setVisibility(View.INVISIBLE);
holder.tvDiscAmt.setVisibility(View.INVISIBLE);
}

if (cartList != null) {
for (Design d : cartList) {
if (design.id == d.id) {
design.pcs = d.pcs;
}
}
}

if (design.pcs != 0) {
holder.llAdd.setVisibility(View.GONE);
holder.llPlusMinus.setVisibility(View.VISIBLE);
holder.btPcsCount.setText(Integer.toString(design.pcs));
}

holder.btAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
holder.llAdd.setVisibility(View.GONE);
holder.llPlusMinus.setVisibility(View.VISIBLE);
if (cartList != null) {
if (cartList.contains(design)) {
cartList.remove(design);
design.pcs = 1;
cartList.add(design);
saveSharedPref(context, "cart", new Gson().toJson(cartList));
((Activity) context).invalidateOptionsMenu();
} else {
design.pcs = 1;
cartList.add(design);
saveSharedPref(context, "cart", new Gson().toJson(cartList));
((Activity) context).invalidateOptionsMenu();
}
} else {
cartList = new ArrayList<Design>(Arrays.asList(design));
cartList.remove(design);
design.pcs = 1;
cartList.add(design);
saveSharedPref(context, "cart", new Gson().toJson(cartList));
((Activity) context).invalidateOptionsMenu();
}
}
});

holder.btPlus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (Integer.parseInt(holder.btPcsCount.getText().toString()) >= 1) {
int a = Integer.parseInt(holder.btPcsCount.getText().toString());
a++;
holder.btPcsCount.setText(Integer.toString(a));
if (cartList != null) {
if (cartList.contains(design)) {
cartList.remove(design);
design.pcs = a;
cartList.add(design);
saveSharedPref(context, "cart", new Gson().toJson(cartList));
((Activity) context).invalidateOptionsMenu();
} else {
design.pcs = a;
cartList.add(design);
saveSharedPref(context, "cart", new Gson().toJson(cartList));
((Activity) context).invalidateOptionsMenu();
}
}
}
}
});

holder.btMinus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (Integer.parseInt(holder.btPcsCount.getText().toString()) <= 1) {
holder.llAdd.setVisibility(View.VISIBLE);
holder.llPlusMinus.setVisibility(View.GONE);
cartList.remove(design);
saveSharedPref(context, "cart", new Gson().toJson(cartList));
((Activity) context).invalidateOptionsMenu();
} else {
int a = Integer.parseInt(holder.btPcsCount.getText().toString());
a--;
holder.btPcsCount.setText(Integer.toString(a));
cartList.remove(design);
design.pcs = a;
cartList.add(design);
saveSharedPref(context, "cart", new Gson().toJson(cartList));
((Activity) context).invalidateOptionsMenu();
}
}
});
}

@Override
public int getItemCount() {
return designList.size();
}

public class ViewHolder extends RecyclerView.ViewHolder {

public LinearLayout llPer, llAdd, llPlusMinus;
public Button btPlus, btMinus, btPcsCount, btAdd;
public ImageView ivDesign;
public TextView tvDesign, tvQuality, tvShade, tvAmount, tvDiscAmt, tvDisPer;

public ViewHolder(View itemView) {
super(itemView);

llAdd = (LinearLayout) itemView.findViewById(R.id.llAdd);
llPlusMinus = (LinearLayout) itemView.findViewById(R.id.llPlusMinus);
btPlus = (Button) itemView.findViewById(R.id.btPlus);
btAdd = (Button) itemView.findViewById(R.id.btAdd);
//other findViewByIds
}
}
}

MainActivity.java

public class MainActivity extends AppCompatActivity {

private RecyclerView recyclerView;
private Single_DesignAdapter adapter;
private ArrayList<Design> designList;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
}

@Override
protected void onResume() {
super.onResume();
designList = new ArrayList<Design>();
designList = list();

/*if (cartList != null) {
String cart = getSharePref(MainActivity.this, "cart");
Design[] designs = new Gson().fromJson(cart, Design[].class);
cartList = (ArrayList<Design>) Arrays.asList(designs);
}*/

recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));
recyclerView.setHasFixedSize(true);
adapter = new Single_DesignAdapter(MainActivity.this, designList);
recyclerView.setAdapter(adapter);

invalidateOptionsMenu();
}

public ArrayList<Design> list() {
ArrayList<Design> arrayList = new ArrayList<Design>();

Design design = new Design();
design.id = 1;
design.name = "Black";
design.image = "http://linksolutions.in/Demo/images/1.jpg";
design.designName = "11001";
design.qualityName = "Cotton";
design.amount = "1000";
design.discPercent = "5";

Design design2 = new Design();
design2.id = 2;
design2.name = "Green";
design2.image = "http://linksolutions.in/Demo/images/2.jpg";
design2.designName = "11001";
design2.qualityName = "Cotton";
design2.amount = "900";
design2.discPercent = "9";

Design design3 = new Design();
design3.id = 3;
design3.name = "Green";
design3.image = "http://linksolutions.in/Demo/images/2.jpg";
design3.designName = "11002";
design3.qualityName = "Satin";
design3.amount = "1200";
design3.discPercent = "12";

arrayList.add(design);
arrayList.add(design2);
arrayList.add(design3);

return arrayList;
}

public static void setBadgeCount(Context context, LayerDrawable icon, int count) {

BadgeDrawable badge;

// Reuse drawable if possible
Drawable reuse = icon.findDrawableByLayerId(R.id.ic_badge);
if (reuse != null && reuse instanceof BadgeDrawable) {
badge = (BadgeDrawable) reuse;
} else {
badge = new BadgeDrawable(context);
}

badge.setCount(count);
icon.mutate();
icon.setDrawableByLayerId(R.id.ic_badge, badge);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

getMenuInflater().inflate(R.menu.cart_menu, menu);
MenuItem itemCart = menu.findItem(R.id.menu_cart);
LayerDrawable icon = (LayerDrawable) itemCart.getIcon();
String list = getSharePref(MainActivity.this, "cart");
if (list != null) {
Design[] designs = new Gson().fromJson(list, Design[].class);
int count = designs.length;
setBadgeCount(MainActivity.this, icon, count);
}
return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {

case R.id.menu_cart:
Intent intent = new Intent(MainActivity.this, CheckOutActivity.class);
startActivity(intent);
return false;
default:
break;
}
return false;

}
}

我以为这段代码会起作用,但事实并非如此。有没有其他方法可以解决这个问题,请建议。

最佳答案

刚刚更改了以下构造函数代码,它就开始工作了。

public Single_DesignAdapter(Context context, ArrayList<Design> designList) {
this.designList = designList;
this.context = context;
this.layoutInflater = LayoutInflater.from(context);

String cart = getSharePref(context, "cart");
Design[] designs = new Gson().fromJson(cart, Design[].class);

if (cartList != null) {
cartList = new ArrayList<Design>(Arrays.asList(designs));
} else {
if (designs != null) {
cartList = new ArrayList<Design>();
for (Design d : designs) {
cartList.add(d);
}
}
}
}

关于java - 添加到购物车设计在 Android 中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43471357/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com