Search in sources :

Example 1 with MenuListAdapter

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);
            }
        }
    });
}
Also used : AlertDialog(android.support.v7.app.AlertDialog) NavigationView(android.support.design.widget.NavigationView) DialogInterface(android.content.DialogInterface) ActionBarDrawerToggle(android.support.v7.app.ActionBarDrawerToggle) ArrayList(java.util.ArrayList) Product(com.example.asus.onlinecanteen.model.Product) Intent(android.content.Intent) NavigationView(android.support.design.widget.NavigationView) View(android.view.View) TextView(android.widget.TextView) ListView(android.widget.ListView) DrawerLayout(android.support.v4.widget.DrawerLayout) MenuListAdapter(com.example.asus.onlinecanteen.adapter.MenuListAdapter) Cart(com.example.asus.onlinecanteen.model.Cart)

Aggregations

DialogInterface (android.content.DialogInterface)1 Intent (android.content.Intent)1 NavigationView (android.support.design.widget.NavigationView)1 DrawerLayout (android.support.v4.widget.DrawerLayout)1 ActionBarDrawerToggle (android.support.v7.app.ActionBarDrawerToggle)1 AlertDialog (android.support.v7.app.AlertDialog)1 View (android.view.View)1 ListView (android.widget.ListView)1 TextView (android.widget.TextView)1 MenuListAdapter (com.example.asus.onlinecanteen.adapter.MenuListAdapter)1 Cart (com.example.asus.onlinecanteen.model.Cart)1 Product (com.example.asus.onlinecanteen.model.Product)1 ArrayList (java.util.ArrayList)1