'popup menu not appearing even after initializing it properly (Android Studio)
so I created a recycler view, and a card view to populate the recycler view. I want to allow the user to click on the cardview, and have the ability to remove an item. I added a popup menu, but it is not appearing. Any suggestions? Thanks in advance.
Code for OnItemClicked(Triggers when a cardview is clicked):
@Override
public void onItemClicked(CartItemsModel cartItemsModel) {
CardView cardView = findViewById(R.id.cart_container);
Context context = CartActivity.this;
PopupMenu popup = new PopupMenu(CartActivity.this,cardView);
popup.getMenuInflater().inflate(R.menu.options_menu, popup.getMenu());
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
Toast.makeText(context, "click registered", Toast.LENGTH_SHORT).show();
switch (menuItem.getItemId()) {
case R.id.menu1:
DatabaseReference database = FirebaseDatabase.getInstance().getReference().child("Orders");
FirebaseAuth auth = FirebaseAuth.getInstance();
String username = getUser(auth.getCurrentUser().getEmail());
Query productIDQuery = database.child(username).orderByChild("ProductID").equalTo(cartItemsModel.getProductID());
productIDQuery.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
for (DataSnapshot productIDsnapshot : snapshot.getChildren()) {
productIDsnapshot.getRef().removeValue();
Toast.makeText(context, "Removed from cart", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
Toast.makeText(context, "error: " + error, Toast.LENGTH_SHORT).show();
}
});
break;
case R.id.menu2:
Toast.makeText(context, "Quantity: " + cartItemsModel.getTotalQuantity(), Toast.LENGTH_SHORT).show();
break;
}
return false;
}
});
}
the menu XML file:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/menu1"
android:title="Remove item" />
<item
android:id="@+id/menu2"
android:title="Get Quantity" />
</menu>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
