简体   繁体   中英

Why cant i get these calculations to display on a textview?

Im trying to make an app that converts distance/area/volume using spinners as a unit selection method. Calculations are meant be done and then the output sent to a textview based on what is entered into the EditText. But the output is only ever 0.0 and nothing else. Can anybody help with this ?

spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
            switch(pos){
            case 0:         
                option1.setAdapter(length);
                option2.setAdapter(length);
                return;
            case 1:         
                option1.setAdapter(area);
                option2.setAdapter(area);
                return;
            default:
        }           
    }

        public void onNothingSelected(AdapterView<?> parent) {
          // Do nothing.
        }
    });

    option1.setOnItemSelectedListener(new OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
            if(pos>=0) {
                stringInput = edittext1.getText().toString();
                if(stringInput == null || stringInput.isEmpty()) {
                    doubleInput = 0.0;
                }
                else {
                    doubleInput = Double.parseDouble(edittext1.getText().toString());
                }                                   
            }                               
        }

        public void onNothingSelected(AdapterView<?> parent) {
          // Do nothing.
        }
    });     

    option2.setOnItemSelectedListener(new OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
            switch(pos) {
            case 0:
                miles = doubleInput * 1;
                textview1.setText("" + String.valueOf(miles));
                return;
            case 1:
                km = doubleInput * 1.609344;
                textview1.setText("" + String.valueOf(km));
                return;
            default:
            }

        }

        public void onNothingSelected(AdapterView<?> parent) {
          // Do nothing.
        }
    });

At the risk of sounding like a really old man, this looks like your college homework... Is it?

Your code has changed since asking the question, which makes it pretty difficult to answer! Luckily I managed to scrape your code into Eclipse before you changed it... Anyway, in your original code your performing all your operations in the create method, at which point you haven't entered a value for edittext1 (unless it's set to some sensible default, which I presume would be 0, hence always getting zero as your answer?)

    // Whilst setting up a view the create method will not have a 
    // reasonable value for edittext1 - or it will be your default
    String stringInput = (edittext1.getText().toString());
if (stringInput.isEmpty()) { 
    doubleInput = 0.0;    // Will always enter this line
} else {
    doubleInput = Double.parseDouble(edittext1.getText().toString());
}

You've duplicated the code...

output1 = (TextView) findViewById(R.id.output1);

Actually output1 through to output10 (ie all ten lines) are duplicated.

As to your updated code, is it still giving you a problem? Are you sure that stringInput has a value? I mean have you typed something in? You could check by debugging your program..

The following is also error prone as FloatingCoder suggests, and is likely to break...

doubleInput = Double.parseDouble(edittext1.getText().toString());

A better way to do this (because it catches the exception that Java might throw) is

doubleInput = 0.0;
String inputStr = question.getText().toString();
try {
    doubleInput = Double.parseDouble(inputStr);
} catch (NumberFormatException e) {
    // This should probably do something more useful? i.e. tell
    // the user what they've done wrong...
    Log.e("Android",
                    "Double throws a NumberFormatException if you enter text that is not a number");
}

Oh and Android has some helper utilities for checking strings, see TextUtils, or just my example...

if (!TextUtils.isEmpty(inputStr)) { // checks for "" and null (see documentation)
      doubleInput = Double.parseDouble(inputStr);
}

I'd REALLY recommend writing a simple test case for a calculation that looks incorrect, because two text boxes and a button really aren't hard to throw together and are seriously easy to debug without the need for all the spinners getting in the way... Anyway hope this helped, oh and my complete example with two edittexts and a button, I'll just post that here... Hope it helps...

private Button btnCalc;
private EditText question, answer;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    answer = (EditText) findViewById(R.id.answer);
    question = (EditText) findViewById(R.id.question);
    btnCalc = (Button) findViewById(R.id.btnCalc);
            // The OnClickListener here will be executed outside the "Create",
            // i.e., when you actually click on the button, which will give you
            // a chance to enter some values in the question edittext...
    btnCalc.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            double in = 0.0;
            try {
                String inputStr = question.getText().toString();

                // if you want to check it use
                if (!TextUtils.isEmpty(inputStr)) {
                    in = Double.parseDouble(inputStr);
                }
            } catch (NumberFormatException e) {
                // This should probably do something more useful? i.e. tell
                // the user what they've done wrong...
                Log.e("Android",
                        "Double throws a NumberFormatException if you enter text that is not a number");
            }

            double miles = in * 1.6;
            answer.setText(String.valueOf(miles));
        }
    });
}

It's a little hard to tell from the code you posted, but my guess is that you are setting doubleInput when option 1 is selected. If you are entering the number after you select option 1, it will always be 0.0, as the number was not in the text area when the spinner was changed.

Use a debugger to step through the code and see if you are ever reaching the line

doubleInput = Double.parseDouble(edittext1.getText().toString());

and to check that the number is getting set properly at that point.

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