Guest User

Untitled

a guest
Sep 21st, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.27 KB | None | 0 0
  1. public class CartAdapter extends RecyclerView.Adapter<CartAdapter.ViewHolder> {
  2.  
  3. private List<ProductView.Data> productData = Collections.emptyList();
  4. static List<ProductModel> productModelList;
  5. static Context context;
  6. DatabaseHandler mDatabaseHandler;
  7.  
  8. public CartAdapter(Context context, List<ProductModel> dbList ){
  9. this.productModelList = new ArrayList<ProductModel>();
  10. this.context = context;
  11. this.productModelList = dbList;
  12. mDatabaseHandler = new DatabaseHandler( context );
  13. }
  14.  
  15. @Override
  16. public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  17. Context context = parent.getContext();
  18. LayoutInflater inflater = LayoutInflater.from(context);
  19.  
  20. // Inflate the custom layout
  21. View cartListView = inflater.inflate(R.layout.list_item_cart, parent, false);
  22.  
  23. // Return a new holder instance
  24. ViewHolder viewHolder = new ViewHolder(context,cartListView);
  25. return viewHolder;
  26. }
  27.  
  28. @Override
  29. public void onBindViewHolder(ViewHolder holder, final int position) {
  30.  
  31. holder.tvProductName.setText(productModelList.get(position).getTitle());
  32. holder.tvProductPrice.setText(productModelList.get(position).getPrice());
  33. Glide
  34. .with(context)
  35. .load(productModelList.get(position).getImageUrl())
  36. .into(holder.imgProduct);
  37. // holder.tvProductStatus.setText(productModelList.get(position).getIsAvailable());
  38. holder.tvSize.setText(productModelList.get(position).getSize());
  39. holder.tvProductQuantity.setText(Integer.toString(productModelList.get(position).getQuantity()));
  40. holder.tvColor.setText(productModelList.get(position).getColor());
  41.  
  42. //holder.tvMaterial.setText(productModelList.get(position).getMaterial());
  43.  
  44. holder.imgDelete.setClickable(true);
  45. holder.imgDelete.setOnClickListener(new View.OnClickListener() {
  46. @Override
  47. public void onClick(View v) {
  48. String idForDelete = String.valueOf(productModelList.get(position).getVariantId());
  49.  
  50. mDatabaseHandler.deleteARow(idForDelete);
  51. productModelList.remove(position);
  52. notifyItemRemoved(position);
  53. notifyItemRangeChanged(position,productModelList.size());
  54. }
  55. });
  56. }
  57.  
  58. @Override
  59. public int getItemCount() {
  60. return productModelList.size();
  61. }
  62.  
  63. public class ViewHolder extends RecyclerView.ViewHolder {
  64. TextView tvProductName, tvProductPrice, tvProductQuantity,tvColor,
  65. tvSize;
  66. ImageView imgProduct;
  67. ImageButton imgDelete;
  68. Context context;
  69.  
  70. public ViewHolder(Context mContext, View itemView) {
  71. super(itemView);
  72.  
  73. this.tvProductName = (TextView) itemView.findViewById(R.id.tv_cart_product_name);
  74. this.tvProductPrice = (TextView) itemView.findViewById(R.id.tv_cart_product_price);
  75. this.tvProductQuantity = (TextView) itemView.findViewById(R.id.tv_cart_product_Quantity);
  76. this.imgProduct = (ImageView) itemView.findViewById(R.id.img_cart_item_product);
  77. this.tvColor = (TextView)itemView.findViewById(R.id.tv_color);
  78. this.tvSize = (TextView) itemView.findViewById(R.id.tv_size);
  79. this.imgDelete = (ImageButton) itemView.findViewById(R.id.img_cart_delete);
  80. // store the context ///
  81. this.context = mContext;
  82. }
  83. }
  84.  
  85. public class CartActivity extends AppCompatActivity {
  86. DatabaseHandler helper;
  87. List<ProductModel> dbList;
  88. RecyclerView mRecyclerView;
  89. Toolbar toolbar;
  90. Button btnCheckout, btnContinueShopping;
  91. TextView tvTotalNoOfItems, tvTotalPrice;
  92. String p;
  93. String i;
  94.  
  95. private RecyclerView.Adapter mAdapter;
  96. private RecyclerView.LayoutManager mLayoutManager;
  97.  
  98. @Override
  99. protected void onCreate(final Bundle savedInstanceState) {
  100. super.onCreate(savedInstanceState);
  101. setContentView(R.layout.activity_cart);
  102.  
  103. p = getIntent().getStringExtra("variant_id");
  104. i = getIntent().getStringExtra("product_id");
  105. Bundle extras = getIntent().getExtras();
  106. if (extras != null) {
  107. p = extras.getString("variant_id");
  108. i= extras.getString("product_id");
  109. }
  110.  
  111. toolbar = (Toolbar) findViewById(R.id.customToolBar);
  112. setSupportActionBar(toolbar);
  113. setTitle("Check-out");
  114. toolbar.setTitleTextColor(Color.BLACK);
  115.  
  116. toolbar.setNavigationIcon(R.drawable.ic_arrow_back_black);
  117. toolbar.setNavigationOnClickListener(new View.OnClickListener() {
  118. @Override
  119. public void onClick(View v) {
  120. onBackPressed();
  121. }
  122. });
  123.  
  124. helper = new DatabaseHandler(this);
  125. dbList= new ArrayList<ProductModel>();
  126. dbList = helper.getDataFromDB();
  127.  
  128. mRecyclerView = (RecyclerView)findViewById(R.id.rv_cart_item_list);
  129.  
  130. // use a linear layout manager
  131. mLayoutManager = new LinearLayoutManager(this);
  132. mRecyclerView.setLayoutManager(mLayoutManager);
  133.  
  134. mAdapter = new CartAdapter(this,dbList);
  135. mRecyclerView.setAdapter(mAdapter);
  136.  
  137. tvTotalNoOfItems = (TextView)findViewById(R.id.tvTotalCartItems);
  138. tvTotalPrice = (TextView)findViewById(R.id.tvTotalCartItemsPrice);
  139. String totalPrice = "";
  140. for (int i = 0; i<dbList.size(); i++)
  141. {
  142. totalPrice = totalPrice + dbList.get(i).getPrice().toString();
  143. }
  144. tvTotalPrice.setText(totalPrice);
  145.  
  146. btnContinueShopping = (Button)findViewById(R.id.btnBackToProductActivity);
  147. btnContinueShopping.setOnClickListener(new View.OnClickListener() {
  148. @Override
  149. public void onClick(View v) {
  150. Intent launchCOllectionActivity = new Intent(CartActivity.this, CollectionActivity.class);
  151. startActivity(launchCOllectionActivity);
  152. finish();
  153. }
  154. });
  155. btnCheckout = (Button)findViewById(R.id.btn_checkout);
  156. btnCheckout.setOnClickListener(new View.OnClickListener() {
  157. @Override
  158. public void onClick(View v) {
  159. Intent launchCheckoutActivity = new Intent(CartActivity.this,CheckoutActivity.class);
  160. startActivity(launchCheckoutActivity);
  161. }
  162. });
  163. }
  164. }
  165.  
  166. public List<ProductModel> getItems(){
  167. return productModelList;
  168. }
  169.  
  170. mAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
  171. @Override
  172. public void onChanged () {
  173. tvTotalNoOfItems.setText(mAdapter.getItemCount());
  174. String totalPrice = "";
  175. for (int i = 0; i < ((CartAdapter)mAdapter).getItems().size(); i++) {
  176. totalPrice = totalPrice + ((CartAdapter)mAdapter).getItems().get(i).getPrice().toString();
  177. }
  178. tvTotalPrice.setText("" + totalPrice);
  179. }
  180. });
  181.  
  182. tvTotalNoOfItems.setText(mAdapter.getCount());
  183.  
  184. String totalPrice = "";
  185. ((CartActivity)context).tvTotalNoOfItems.setText(getCount());
  186. for (int i = 0; i<productModelList.size(); i++)
  187. {
  188. totalPrice = totalPrice + productModelList.get(i).getPrice().toString();
  189. }
  190. ((CartActivity)context).tvTotalPrice.setText(""+totalPrice);
  191.  
  192. public TextView tvTotalNoOfItems, tvTotalPrice;
Add Comment
Please, Sign In to add comment