2012-01-04 61 views
1

我试图制作一个应用程序,使用spinners将距离/面积/体积转换为单位选择方法。计算意味着完成,然后根据输入到EditText中的内容将输出发送到textview。但是输出只有0.0而没有别的。任何人都可以帮忙吗?为什么不能让这些计算显示在textview上?

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

回答

1

虽然听起来像一个真正的老头的风险,这看起来像你的大学功课......是吗?

你的代码,因为问的问题,这使得它非常难以回答的改变!幸运的是,我设法争取到你的代码到Eclipse中你改变了它之前...无论如何,在你原来的代码你在创建方法执行所有操作,在这一点,除非它被设置为一些你没有输入edittext1值(合理的缺省值,我相信会是0,因此总是让零作为你的答案?)

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

你复制的代码...

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

其实OUTPUT1通过对output10(即所有十行)被复制。

至于你更新的代码,它仍然给你一个问题吗?你确定stringInput有一个值吗?我的意思是你输入了什么?您可以通过调试程序检查..

下也容易出错的FloatingCoder建议,并有可能打破...

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

一个更好的办法来做到这一点(因为它捕捉异常是Java的可能抛出)就是

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

哦,Android有检查字符串一些帮助工具,看文本实用程序,或者只是我举的例子...

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

我真的建议编写一个简单的测试案例的计算,看起来不正确的,因为两个文本框和一个按钮真的是不难扔在一起,并认真容易,而不需要所有的纺纱厂在获得调试的方式......总之希望这有助于,噢,我有两个edittexts和一个按钮完整的例子,我就张贴在这里...希望它可以帮助...

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)); 
     } 
    }); 
} 
+0

谢谢了很多这有助于大规模。那么它不是我的大学工作,但我才刚刚开始在大学学习Java和我一直想,因为得到一个Nexus One的,所以我想ID必须在东西比所提供的网络上最教程更复杂去开发Android。再次感谢 – 2012-01-05 01:07:31

+0

没问题...ActionBar没有做任何有用的事情......如果这不是Uni/College作业,你可能会发现RoboGuice对你的android应用程序有用......然后,如果你刚刚学习Java可能是太过分了。 Nexus One的开发也意味着您可能无法使用ICS。开发早期版本的Android在我看来令人沮丧!祝你好运! – 0909EM 2012-01-05 01:15:56

+0

Ive在我的nexus one 4.0.3上获得了ICS,与XDA Developers的开发者完全相同。所以我想为什么不先开发ICS然后让它在老版本的Android上工作。再次感谢 – 2012-01-05 01:21:23

0

这一点很难从您发布的代码告诉,但我的猜测是,选择选项1,当您正在设置doubleInput。如果您在选择选项1后输入数字,它将始终为0.0,因为当更改微调器时,数字不在文本区域中。

使用调试器单步执行代码,看看你曾经到达线

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

,并检查数量越来越在这一点上正确设置。

相关问题