如何为Android中的列表中的每个第三项设置提醒?

时间:2014-03-26 11:35:44

标签: android arraylist

我有一个列表,可以通过Android中的数据库从另一个列表中添加项目。但我需要为每个第三项设置一个提醒,即3,6,9,12等。当我这样做时

if( types.size() == 3 && types.size() == 6 && types.size() ==9) 

我的申请意外结束。以下是我的代码。当我只有一个数字时,它工作,但我需要所有第三个奇数。任何提示对我来说都很有价值,谢谢

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();
    }
    }

3 个答案:

答案 0 :(得分:2)

改变这个:

if( types.size() == 3 && types.size() == 6 && types.size() ==9) 

到此:

if(types.size() % 3 == 0)

这使用modulo,它检查可分割性,如果可以被该数字除,则返回0。 http://en.wikipedia.org/wiki/Modulo_operation

答案 1 :(得分:1)

我应该这样做:

   if( types.size() % 3 == 0) {
         // do something special
    } else {
         // do something normal
    }

答案 2 :(得分:0)

你只需检查它的大小是否可分为/ 3或不足够