简体   繁体   中英

How to stop looping and insert data to array in certain conditon on Android

I want to make my looping stop when it's on the condition Here is the code :

for (i = 0; i < 8 ; i++) {
    Cursor r = db.rawQuery("SELECT _id, key_foodstuff, key_calorie, key_carbohydrate, key_fat, key_protein FROM (food INNER JOIN categories ON food.key_nocategory = categories.nocategories) WHERE key_type!='secondary' AND _id!=164 ORDER BY RANDOM() LIMIT 1", null);       

    if(r.moveToFirst())
    {
            morning_food[i] = r.getString(0);
            Log.v("_id : ", morning_food[i]);
            morning_food[c] = r.getString(2);
            Log.v("Kalori : ", morning_food[c]);
            sumOfCalorie = sumOfCalorie + Double.parseDouble(morning_food [c]);
            Log.v("Sum : ", "" + sumOfCalorie);

            if (sumOfCalorie == morning_cal || sumOfCalorie >= morning_cal-(morning_cal*0.1) && sumOfCalorie <= morning_cal+(morning_cal*0.1))
            {
                break;
            }


    }

is there any solution for this? it is not work ... Thx.

if(r.moveToFirst())..您正在看第一行8次却从未移至下一行?

try this

outerloop:
for (i = 0; i < 8 ; i++) {
    Cursor r = db.rawQuery("SELECT _id, key_foodstuff, key_calorie, key_carbohydrate, key_fat, key_protein FROM (food INNER JOIN categories ON food.key_nocategory = categories.nocategories) WHERE key_type!='secondary' AND _id!=164 ORDER BY RANDOM() LIMIT 1", null);       

    if(r.moveToFirst())
    {
            morning_food[i] = r.getString(0);
            Log.v("_id : ", morning_food[i]);
            morning_food[c] = r.getString(2);
            Log.v("Kalori : ", morning_food[c]);
            sumOfCalorie = sumOfCalorie + Double.parseDouble(morning_food [c]);
            Log.v("Sum : ", "" + sumOfCalorie);

            if (sumOfCalorie == morning_cal || sumOfCalorie >= morning_cal-(morning_cal*0.1) && sumOfCalorie <= morning_cal+(morning_cal*0.1))
            {
                break outerloop;
            }


    }

Can you place this code in a function like:

            private void doYourCode() {
            for (i = 0; i < 8 ; i++) {
                Cursor r = db.rawQuery("SELECT _id, key_foodstuff, key_calorie, key_carbohydrate, key_fat, key_protein FROM (food INNER JOIN categories ON food.key_nocategory = categories.nocategories) WHERE key_type!='secondary' AND _id!=164 ORDER BY RANDOM() LIMIT 1", null);       

                if(r.moveToFirst())
                {
                        morning_food[i] = r.getString(0);
                        Log.v("_id : ", morning_food[i]);
                        morning_food[c] = r.getString(2);
                        Log.v("Kalori : ", morning_food[c]);
                        sumOfCalorie = sumOfCalorie + Double.parseDouble(morning_food [c]);
                        Log.v("Sum : ", "" + sumOfCalorie);

                        if (sumOfCalorie == morning_cal || sumOfCalorie >= morning_cal-(morning_cal*0.1) && sumOfCalorie <= morning_cal+(morning_cal*0.1))
                        {
                            //break;
                            return;
                        }


                }
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM