Reputation: 83
I have a list which adds up item from another list through database in Android. But I need to set up a reminder for every third item, that is 3, 6, 9 , 12, etc. When I do like this
if( types.size() == 3 && types.size() == 6 && types.size() ==9)
my appliction closes unexpectedly. Below is my code. Its working when I only have one number, but I need it for all the third odd numbers. Any tips would be really valuable for me, Thank you
private List<AlcoholTypes> types = new ArrayList<AlcoholTypes>();
types = Database.getDataList(this);
if(types.size() == 3 ){
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
// set title
alertDialogBuilder.setTitle("Water Reminder");
// set dialog message
alertDialogBuilder
.setMessage("Drink one glass of water !")
.setCancelable(false)
.setPositiveButton("Okay",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, close
// current activity
dialog.cancel();
}
})
.setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
}
Upvotes: 0
Views: 96
Reputation: 21
This should do it I guess:
if( types.size() % 3 == 0) {
// do something special
} else {
// do something normal
}
Upvotes: 1
Reputation: 316
Change this:
if( types.size() == 3 && types.size() == 6 && types.size() ==9)
to this:
if(types.size() % 3 == 0)
This makes use of modulo, which checks divisability and returns 0 if it is divisable by that number. http://en.wikipedia.org/wiki/Modulo_operation
Upvotes: 2
Reputation: 1442
You just check it size is divisible /3 or not that is enough
Upvotes: 0