Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class CartAdapter extends RecyclerView.Adapter<CartAdapter.ViewHolder> {
- private List<ProductView.Data> productData = Collections.emptyList();
- static List<ProductModel> productModelList;
- static Context context;
- DatabaseHandler mDatabaseHandler;
- public CartAdapter(Context context, List<ProductModel> dbList ){
- this.productModelList = new ArrayList<ProductModel>();
- this.context = context;
- this.productModelList = dbList;
- mDatabaseHandler = new DatabaseHandler( context );
- }
- @Override
- public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
- Context context = parent.getContext();
- LayoutInflater inflater = LayoutInflater.from(context);
- // Inflate the custom layout
- View cartListView = inflater.inflate(R.layout.list_item_cart, parent, false);
- // Return a new holder instance
- ViewHolder viewHolder = new ViewHolder(context,cartListView);
- return viewHolder;
- }
- @Override
- public void onBindViewHolder(ViewHolder holder, final int position) {
- holder.tvProductName.setText(productModelList.get(position).getTitle());
- holder.tvProductPrice.setText(productModelList.get(position).getPrice());
- Glide
- .with(context)
- .load(productModelList.get(position).getImageUrl())
- .into(holder.imgProduct);
- // holder.tvProductStatus.setText(productModelList.get(position).getIsAvailable());
- holder.tvSize.setText(productModelList.get(position).getSize());
- holder.tvProductQuantity.setText(Integer.toString(productModelList.get(position).getQuantity()));
- holder.tvColor.setText(productModelList.get(position).getColor());
- //holder.tvMaterial.setText(productModelList.get(position).getMaterial());
- holder.imgDelete.setClickable(true);
- holder.imgDelete.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- String idForDelete = String.valueOf(productModelList.get(position).getVariantId());
- mDatabaseHandler.deleteARow(idForDelete);
- productModelList.remove(position);
- notifyItemRemoved(position);
- notifyItemRangeChanged(position,productModelList.size());
- }
- });
- }
- @Override
- public int getItemCount() {
- return productModelList.size();
- }
- public class ViewHolder extends RecyclerView.ViewHolder {
- TextView tvProductName, tvProductPrice, tvProductQuantity,tvColor,
- tvSize;
- ImageView imgProduct;
- ImageButton imgDelete;
- Context context;
- public ViewHolder(Context mContext, View itemView) {
- super(itemView);
- this.tvProductName = (TextView) itemView.findViewById(R.id.tv_cart_product_name);
- this.tvProductPrice = (TextView) itemView.findViewById(R.id.tv_cart_product_price);
- this.tvProductQuantity = (TextView) itemView.findViewById(R.id.tv_cart_product_Quantity);
- this.imgProduct = (ImageView) itemView.findViewById(R.id.img_cart_item_product);
- this.tvColor = (TextView)itemView.findViewById(R.id.tv_color);
- this.tvSize = (TextView) itemView.findViewById(R.id.tv_size);
- this.imgDelete = (ImageButton) itemView.findViewById(R.id.img_cart_delete);
- // store the context ///
- this.context = mContext;
- }
- }
- public class CartActivity extends AppCompatActivity {
- DatabaseHandler helper;
- List<ProductModel> dbList;
- RecyclerView mRecyclerView;
- Toolbar toolbar;
- Button btnCheckout, btnContinueShopping;
- TextView tvTotalNoOfItems, tvTotalPrice;
- String p;
- String i;
- private RecyclerView.Adapter mAdapter;
- private RecyclerView.LayoutManager mLayoutManager;
- @Override
- protected void onCreate(final Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_cart);
- p = getIntent().getStringExtra("variant_id");
- i = getIntent().getStringExtra("product_id");
- Bundle extras = getIntent().getExtras();
- if (extras != null) {
- p = extras.getString("variant_id");
- i= extras.getString("product_id");
- }
- toolbar = (Toolbar) findViewById(R.id.customToolBar);
- setSupportActionBar(toolbar);
- setTitle("Check-out");
- toolbar.setTitleTextColor(Color.BLACK);
- toolbar.setNavigationIcon(R.drawable.ic_arrow_back_black);
- toolbar.setNavigationOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- onBackPressed();
- }
- });
- helper = new DatabaseHandler(this);
- dbList= new ArrayList<ProductModel>();
- dbList = helper.getDataFromDB();
- mRecyclerView = (RecyclerView)findViewById(R.id.rv_cart_item_list);
- // use a linear layout manager
- mLayoutManager = new LinearLayoutManager(this);
- mRecyclerView.setLayoutManager(mLayoutManager);
- mAdapter = new CartAdapter(this,dbList);
- mRecyclerView.setAdapter(mAdapter);
- tvTotalNoOfItems = (TextView)findViewById(R.id.tvTotalCartItems);
- tvTotalPrice = (TextView)findViewById(R.id.tvTotalCartItemsPrice);
- String totalPrice = "";
- for (int i = 0; i<dbList.size(); i++)
- {
- totalPrice = totalPrice + dbList.get(i).getPrice().toString();
- }
- tvTotalPrice.setText(totalPrice);
- btnContinueShopping = (Button)findViewById(R.id.btnBackToProductActivity);
- btnContinueShopping.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Intent launchCOllectionActivity = new Intent(CartActivity.this, CollectionActivity.class);
- startActivity(launchCOllectionActivity);
- finish();
- }
- });
- btnCheckout = (Button)findViewById(R.id.btn_checkout);
- btnCheckout.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Intent launchCheckoutActivity = new Intent(CartActivity.this,CheckoutActivity.class);
- startActivity(launchCheckoutActivity);
- }
- });
- }
- }
- public List<ProductModel> getItems(){
- return productModelList;
- }
- mAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
- @Override
- public void onChanged () {
- tvTotalNoOfItems.setText(mAdapter.getItemCount());
- String totalPrice = "";
- for (int i = 0; i < ((CartAdapter)mAdapter).getItems().size(); i++) {
- totalPrice = totalPrice + ((CartAdapter)mAdapter).getItems().get(i).getPrice().toString();
- }
- tvTotalPrice.setText("" + totalPrice);
- }
- });
- tvTotalNoOfItems.setText(mAdapter.getCount());
- String totalPrice = "";
- ((CartActivity)context).tvTotalNoOfItems.setText(getCount());
- for (int i = 0; i<productModelList.size(); i++)
- {
- totalPrice = totalPrice + productModelList.get(i).getPrice().toString();
- }
- ((CartActivity)context).tvTotalPrice.setText(""+totalPrice);
- public TextView tvTotalNoOfItems, tvTotalPrice;
Add Comment
Please, Sign In to add comment