use of com.example.asus.onlinecanteen.adapter.MenuListAdapter in project OnlineCanteen by josephgunawan97.
the class MainActivity method onCreate.
@Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(R.style.AppTheme);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize Navigation View
DrawerLayout drawerLayout = findViewById(R.id.main_drawer_layout);
ActionBarDrawerToggle drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.navigation_open, R.string.navigation_close);
drawerLayout.addDrawerListener(drawerToggle);
drawerToggle.syncState();
NavigationView navigationView = findViewById(R.id.main_navigation_view);
navigationView.setNavigationItemSelectedListener(new MainNavigationListener());
// Get User
firebaseAuth = FirebaseAuth.getInstance();
user = firebaseAuth.getCurrentUser();
// Initialize References
databaseUsers = FirebaseDatabase.getInstance().getReference("users");
databaseProducts = FirebaseDatabase.getInstance().getReference("products");
databaseStore = FirebaseDatabase.getInstance().getReference("store");
databaseWallet = FirebaseDatabase.getInstance().getReference("wallet");
// Product List
ArrayList<Product> productArrayList = new ArrayList<>();
menuListAdapter = new MenuListAdapter(this, productArrayList);
// Initialize ListView
productListView = findViewById(R.id.list);
productListView.setAdapter(menuListAdapter);
// Cart Button and Click Handler
placeOrder = findViewById(R.id.OrderButton);
placeOrder.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
cart = menuListAdapter.getList();
// Check empty cart
boolean emptyCart = true;
for (Cart c : cart) {
if (c.getQuantity() != 0) {
emptyCart = false;
break;
}
}
if (emptyCart == true) {
// Alert dialog if there are no items in cart
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setMessage("You don't have any items in your cart!").setCancelable(false).setNegativeButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.setTitle("Error");
alert.show();
} else {
// Remove item if qty is 0
ArrayList<Cart> toRemove = new ArrayList<>();
for (Cart c : cart) {
if (c.getQuantity() == 0) {
toRemove.add(c);
}
}
cart.removeAll(toRemove);
// Go to cart
Intent intent = new Intent(MainActivity.this, CartActivity.class);
intent.putExtra("Cart", cart);
intent.putExtra("Seller", menuListAdapter.getSeller());
startActivity(intent);
}
}
});
}
Aggregations