سؤال

The SeekBar in my app is not producing the correct result. The SeekBar starts from 0 so sbSyst is added 70 to each value and sbDias is added 40 to each value.

Partial code:

public void ShowText() {
        int inOne = sbSyst.getProgress();
        int inTwo = sbDias.getProgress();

        if ((inOne >= 0 && inOne <= 20) && (inTwo >= 0 && inTwo <= 20)) {
            tvRes.setText("LOW");
            tvRes.setBackgroundColor(Color.parseColor("#7659F5"));
        }
        else if ((inOne >= 21 && inOne <= 50) && (inTwo >= 21 && inTwo <= 40)) {
            tvRes.setText("IDEAL");
            tvRes.setBackgroundColor(Color.parseColor("#0BA319"));
        }
        else if ((inOne >= 51 && inOne <= 70) && (inTwo >= 41 && inTwo <= 50)) {
            tvRes.setText("PRE-HIGH");
            tvRes.setBackgroundColor(Color.parseColor("#D7EF0C"));
        }
        else if ((inOne >= 71 && inOne <= 120) && (inTwo >= 51 && inTwo <= 60)) {
            tvRes.setText("HIGH");
            tvRes.setBackgroundColor(Color.parseColor("#F70729"));
        }
    }

According to the above code, the following should produce "HIGH" as the text, instead, it is displaying "IDEAL"

enter image description here

Here is the Pastebin for the entire code: http://pastebin.com/C91SpbBg

Please help me resolve this.

UPDATE: added the following and it's working 100%

public void ShowText() {
    int inOne = sbSyst.getProgress() + 70;
    int inTwo = sbDias.getProgress() + 40;

    Log.i("SYSTOLIC SHOWTEXT", String.valueOf(inOne));
    Log.i("DIASTOLIC SHOWTEXT", String.valueOf(inTwo));

    if ((inOne >= 70 && inOne <= 90) && (inTwo >= 40 && inTwo <= 60)) {
        tvRes.setText("LOW");
        tvRes.setBackgroundColor(Color.parseColor("#7659F5"));
    }
    if ((inOne >= 70 && inOne <= 90) && (inTwo >= 61 && inTwo <= 80)) {
        tvRes.setText("IDEAL");
        tvRes.setBackgroundColor(Color.parseColor("#0BA319"));
    }
    if ((inOne >= 70 && inOne <= 90) && (inTwo >= 81 && inTwo <= 90)) {
        tvRes.setText("PRE-HIGH");
        tvRes.setBackgroundColor(Color.parseColor("#C9DF0B"));
    }
    if ((inOne >= 70 && inOne <= 90) && (inTwo >= 91 && inTwo <= 100)) {
        tvRes.setText("HIGH");
        tvRes.setBackgroundColor(Color.parseColor("#F70729"));
    }

    if ((inOne >= 91 && inOne <= 120) && (inTwo >= 40 && inTwo <= 80)) {
        tvRes.setText("IDEAL");
        tvRes.setBackgroundColor(Color.parseColor("#0BA319"));
    }
    if ((inOne >= 91 && inOne <= 120) && (inTwo >= 81 && inTwo <= 90)) {
        tvRes.setText("PRE-HIGH");
        tvRes.setBackgroundColor(Color.parseColor("#C9DF0B"));
    }
    if ((inOne >= 91 && inOne <= 120) && (inTwo >= 91 && inTwo <= 100)) {
        tvRes.setText("HIGH");
        tvRes.setBackgroundColor(Color.parseColor("#F70729"));
    }

    if ((inOne >= 121 && inOne <= 140) && (inTwo >= 40 && inTwo <= 90)) {
        tvRes.setText("PRE-HIGH");
        tvRes.setBackgroundColor(Color.parseColor("#C9DF0B"));
    }
    if ((inOne >= 121 && inOne <= 140) && (inTwo >= 91 && inTwo <= 100)) {
        tvRes.setText("HIGH");
        tvRes.setBackgroundColor(Color.parseColor("#F70729"));
    }

    if ((inOne >= 141 && inOne <= 190) && (inTwo >= 40 && inTwo <= 100)) {
        tvRes.setText("HIGH");
        tvRes.setBackgroundColor(Color.parseColor("#F70729"));
    }
}
هل كانت مفيدة؟

المحلول

Based on the above image, the values of inOne=82 and inTwo=26. Now Looking at your code, HIGH is displayed:

if ((inOne >= 71 && inOne <= 120) && (inTwo >= 51 && inTwo <= 60))

This is not true, since your are using the && operator. Which means both should be in the range of high. This is the problem.

Solution: Think about how you want to deal with the situation when one of the values is in the High range and one in the Low range etc. etc. Then write your logic accordingly. (Example: You could also use the || operator, meaning if either one is 'HIGH` show high.)

Update: Here is a good source where you can read about different if/else rules about interpreting blood pressure readings.

Update 2: I had some free time. Here is an implementation of the showText() method based on the mentioned link:

public void ShowText() {
    int inOne = sbSyst.getProgress() + 70;
    int inTwo = sbDias.getProgress() + 40;

    if((inOne >= 140) || (inTwo >= 90)) {
        // condition 1: if 140 or more systolic or 90 or more diastolic means HIGH BP
        tvRes.setText("HIGH");
        tvRes.setBackgroundColor(Color.parseColor("#F70729"));
    }
    else if((inOne <= 90) || (inTwo <= 60)){
        // (condition 1 is not met)
        // condition 2: if 90 or less systolic or 60 or less diastolic means LOW BP
        tvRes.setText("LOW");
        tvRes.setBackgroundColor(Color.parseColor("#7659F5"));
    }
    else if((inOne >= 120) || (inTwo >= 80)) {
        // (condition 1 and 2 are not met ==> NOT HIGH, NOT LOW)
        // condition 3: if 120 or more systolic or 80 or more diastolic means PRE-HIGH BP
        tvRes.setText("PRE-HIGH");
        tvRes.setBackgroundColor(Color.parseColor("#D7EF0C"));
    }
    else if((inOne >= 90) || (inTwo >= 60)) {
        // (condition 1, 2 and 3  are not met ==> NOT HIGH, NOT LOW, NOT PRE-HIGH)
        // condition 4: if 90 or more systolic or 60 or more diastolic means IDEAL BP
        tvRes.setText("IDEAL");
        tvRes.setBackgroundColor(Color.parseColor("#0BA319"));
    }
}

نصائح أخرى

There is a logical bug in your code. None of the if clauses are getting executes since you use the && operator.

Here is why :

inOne = 82 and inTwo =26

 else if ((inOne >= 71 && inOne <= 120) && (inTwo >= 51 && inTwo <= 60))

In the above if condition, it checks whether inOne lies between 71 and 120, here it does as inOne is 82. So that becomes true. Now your second condition is where it all fails.

It checks whether inTwo is between 51 and 60 which it is not because the value is 26, so this returns false. Your if condition is now :

 if((true) && (false)){
 // Set Text HIGH
 }

Since you used the && operator, this condition will be executed only if both conditions will be true which is not in your case. So it never executes the code within it !

I would implement this as a flexible solution of a 2D matrix. Where each row corresponds to a range of the systolic, each column in each row corresponds to a range of diastolic, and the value in the matrix (row, column) is the resulting value.....

Given systolic find the row that corresponds, within the row find the column that corresponds using diastolic and read out value.

Then map value to text/color for box with result.

This would allow you to have multiple mappings for different genders, ages, weights, ethnicity....and just one code, plus combinations impossible to model with your AND / OR code.....

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top