2014-03-26 96 views
0

我有一個列表,它通過Android中的數據庫從另一個列表中添加項目。不過,我需要設置一個提醒每一個第三項,即3,6,9,12,等等。當我做這樣的如何在Android中的List中爲每個第三項設置提醒?

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

回答

2

更改此:

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

這樣:

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

這使用模數,它檢查可分性並返回0,如果它可以被該數字整除。 http://en.wikipedia.org/wiki/Modulo_operation

+0

謝謝你,它的工作:) – Sindre

+0

請把它標記爲答案,如果你能給予好評它,這將有助於未來的觀衆。 –

0

你剛纔檢查它的大小整除/ 3或不是足夠

1

這應該做到這一點我想:

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

謝謝,這個公式工作:) – Sindre

相關問題