Guest User

Untitled

a guest
Nov 22nd, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. public class CheckOut extends AppCompatActivity {
  2. // Global Variables
  3. Vegetable_DataSource mDataSource_veg;
  4. Vegetable_DataItem veg_item;
  5. ShoppingCart_DataSource mDataSource_cart;
  6. List<ShoppingCart_DataItem> cart_ListFromDB;
  7. ShoppingCart_ItemAdaptor adapter;
  8. Float finalNet_sum;
  9. Float finalNet_price_value;
  10.  
  11. @Override
  12. protected void onCreate(Bundle savedInstanceState) {
  13. super.onCreate(savedInstanceState);
  14. setContentView(R.layout.activity_checkout);
  15. // Using this, as I wished to use onResume()
  16. update_data();
  17. adapter = new ShoppingCart_ItemAdaptor(getApplicationContext(), cart_ListFromDB);
  18.  
  19. ListView listView = (ListView) this.findViewById(R.id.shoppingList);
  20. listView.setAdapter(adapter);
  21.  
  22. listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  23. @Override
  24. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  25. // This function is updating cart_ListFromDB and calling adaptor.notifyDataSetChanged()
  26. show_itemSelector_dialog(position);
  27. }
  28. });
  29. }
  30. private void show_itemSelector_dialog(int position) {
  31. // Here, Entries in database are updated as per input from user.
  32. // Entries in database are updated
  33. // update_data fetches the updated cart_ListFromDB from database.
  34. update_data();
  35. adapter.notifyDataSetChanged();
  36. }
  37.  
  38. private void update_data() {
  39. mDataSource_cart = new ShoppingCart_DataSource(getApplicationContext());
  40. mDataSource_cart.open();
  41. cart_ListFromDB = mDataSource_cart.GetAllItems_shopping_cart();
  42. mDataSource_cart.close();
  43. }
  44. }
  45.  
  46. public class ShoppingCart_ItemAdaptor extends ArrayAdapter {
  47. private List<ShoppingCart_DataItem> mDataItems;
  48. private LayoutInflater mInflator;
  49.  
  50. public ShoppingCart_ItemAdaptor(@NonNull Context context, List<ShoppingCart_DataItem> objects) {
  51. super(context, R.layout.item_checkout, objects);
  52. // mDataItems is being updated only when adaptor is created.
  53. mDataItems = objects;
  54. mInflator = LayoutInflater.from(context);
  55. }
  56.  
  57. @NonNull
  58. @Override
  59. public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
  60. this.pref_login = this.getContext().getSharedPreferences(SESSION_SHARED_PREF, PRIVATE_MODE);
  61. if (convertView == null) {
  62. convertView = mInflator.inflate(R.layout.item_checkout, parent, false);
  63. }
  64.  
  65. ShoppingCart_DataItem item = mDataItems.get(position);
  66.  
  67. // Updating the view items as per "item"
  68.  
  69. return convertView;
  70. }
  71. }
Add Comment
Please, Sign In to add comment