2012-01-04 2 views
1

私は単位選択方法としてスピナーを使って距離/面積/音量を変換するアプリを作ろうとしています。計算が完了した後、出力はEditTextに入力された内容に基づいてテキストビューに送信されます。しかし、出力はたった今0.0しかなく、他には何もありません。誰でもこれを助けることができますか?なぜこれらの計算をテキストビューに表示できないのですか?

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にコードをスクラップすることはできました...とにかく、元のコードでは、createtext1の値を入力していないcreateメソッドのすべての操作を実行します(ただし、私は推測賢明なデフォルトでは、したがって、常にあなたの答えとしてゼロを取得し、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()); 
} 

... output10に至るまで実際に出力1

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

(すなわち、すべての10行)が複製されます。

更新されたコードについては、それでも問題はありますか? 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"); 
} 

OHである)投げるかもしれないとAndroidは、文字列をチェックするためのいくつかのヘルパーユーティリティを持っている、Textutilsなど、またはちょうど私の例を参照することを

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

2つのテキストボックスとボタンが本当に一緒に投げるのが難しくなく、すべてのスピナーが入ってこなくても、デバッグが非常に簡単であるため、誤った計算のための簡単なテストケースを書くことをおすすめします道が...とにかくああ、これは助けを期待し、2 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はウェブ上で提供されているほとんどのチュートリアルよりも複雑です。再度ありがとう –

+0

問題はありません...ActionBarは何も役に立たなかった... Uni/Collegeの宿題でなければ、RoboGuiceがあなたのアンドロイドアプリに役立つかもしれません...あなたのちょうど学習しているJavaがあまりにも遠すぎるかもしれません。また、Nexus Oneを開発すると、おそらくICSを使用できなくなります。以前のバージョンのAndroid用に開発したのは私の意見でした。がんばろう! – 0909EM

+0

Iveは、ネクサス1 4.0.3でICSをXDAデベロッパーの開発者から正確に入手しました。だから私はまずICSのために開発していない、なぜそれがアンドロイドの古いバージョンで動作するようになると思った。再度、感謝します –

0

投稿したコードからは少し難しいですが、オプション1を選択したときにdoubleInputを設定していると思います。オプション1を選択した後に番号を入力する場合は、スピンナーが変更されたときに番号がテキスト領域にないため、常に0.0になります。

コードをステップし、あなたが今までのラインに

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

に達しているかどうかを確認し、数はその時点で適切に設定取得されていることを確認するためにデバッガを使用してください。

関連する問題