添加到购物车设计工作在android

时间:2017-04-18 11:51:16

标签: java android android-recyclerview recycler-adapter

我正在研究应用程序,它与电子商务应用程序类似。用户可以将产品添加到购物车 我在下面提到了两个数组列表。

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

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

此活动是在recyclerview中显示产品。在这里我已经在购物车中添加了两个项目。

enter image description here

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

enter image description here

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

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;

    }
}

我认为这段代码可行,但事实并非如此。请问有什么其他方法可以解决这个问题。

2 个答案:

答案 0 :(得分:0)

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

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);
            }
        }
    }
}

答案 1 :(得分:0)

在购物应用程序中添加到购物车方法。

addtocart=(Button)findViewById(R.id.addtocartbtn);
        addtocart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Cart cart=new Cart();
                cart.setId(id);
                cart.setImageid(imageurl);
                cart.setName(prname);
                cart.setPrice(prprice);
               if (MainActivity.myDatabase.cartDao().isAddToCart(id)!=1){
                   MainActivity.myDatabase.cartDao().addToCart(cart);
               }else {
                   Toast.makeText(AddTocartActivity.this, "You are Already added to cart!", Toast.LENGTH_SHORT).show();

               }
            }
        });

如果您想了解有关添加到android中的购物车的更多信息,请访问Add to cart