Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class CheckOut extends AppCompatActivity {
- // Global Variables
- Vegetable_DataSource mDataSource_veg;
- Vegetable_DataItem veg_item;
- ShoppingCart_DataSource mDataSource_cart;
- List<ShoppingCart_DataItem> cart_ListFromDB;
- ShoppingCart_ItemAdaptor adapter;
- Float finalNet_sum;
- Float finalNet_price_value;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_checkout);
- // Using this, as I wished to use onResume()
- update_data();
- adapter = new ShoppingCart_ItemAdaptor(getApplicationContext(), cart_ListFromDB);
- ListView listView = (ListView) this.findViewById(R.id.shoppingList);
- listView.setAdapter(adapter);
- listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
- @Override
- public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
- // This function is updating cart_ListFromDB and calling adaptor.notifyDataSetChanged()
- show_itemSelector_dialog(position);
- }
- });
- }
- private void show_itemSelector_dialog(int position) {
- // Here, Entries in database are updated as per input from user.
- // Entries in database are updated
- // update_data fetches the updated cart_ListFromDB from database.
- update_data();
- adapter.notifyDataSetChanged();
- }
- private void update_data() {
- mDataSource_cart = new ShoppingCart_DataSource(getApplicationContext());
- mDataSource_cart.open();
- cart_ListFromDB = mDataSource_cart.GetAllItems_shopping_cart();
- mDataSource_cart.close();
- }
- }
- public class ShoppingCart_ItemAdaptor extends ArrayAdapter {
- private List<ShoppingCart_DataItem> mDataItems;
- private LayoutInflater mInflator;
- public ShoppingCart_ItemAdaptor(@NonNull Context context, List<ShoppingCart_DataItem> objects) {
- super(context, R.layout.item_checkout, objects);
- // mDataItems is being updated only when adaptor is created.
- mDataItems = objects;
- mInflator = LayoutInflater.from(context);
- }
- @NonNull
- @Override
- public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
- this.pref_login = this.getContext().getSharedPreferences(SESSION_SHARED_PREF, PRIVATE_MODE);
- if (convertView == null) {
- convertView = mInflator.inflate(R.layout.item_checkout, parent, false);
- }
- ShoppingCart_DataItem item = mDataItems.get(position);
- // Updating the view items as per "item"
- return convertView;
- }
- }
Add Comment
Please, Sign In to add comment