2014-05-05 81 views
0

這個androi應用程序的主要目的是學習如何使用spinners和解析數據。我嘗試了很多東西,但仍然無法正常工作。我沒有在屏幕上得到resultFuel的輸出。在TextView中無法獲得雙輸出

下面的代碼:

public class MainActivity extends Activity implements OnItemSelectedListener { 

Spinner spinFrom,spinTo; 

String[] paths ={"liters/100km","liters/100 miles","(US)gallons/100 miles","(US)gallons/100km","(UK)gallons/100miles", 
     "(UK)gallons/100km","kilometers/liter","miles/liter", "miles/gallon", "kilometers/gallon" 
}; 

EditText etfrom; 
TextView textview; 
double fromFuel,resultFuel; 
Button clik; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    spinFrom = (Spinner) findViewById(R.id.fromSpinner); 
    spinTo = (Spinner) findViewById(R.id.toSpinner); 

    etfrom = (EditText) findViewById(R.id.fromET); 
    textview = (TextView) findViewById(R.id.Results); 
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_spinner_item,paths); 
    spinFrom.setAdapter(adapter); 
    spinTo.setAdapter(adapter); 

    spinFrom.setOnItemSelectedListener(this); 
    spinTo.setOnItemSelectedListener(this); 

    if(etfrom.length() == 0 || etfrom.equals("") || etfrom == null){ 

    }else{ 


    } 
    clik = (Button) findViewById(R.id.button1); 
    clik.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      if(selectedFrom.equals(selectedTo)){ 
       //Do nothing 
      }else if(selectedFrom.equals("liters/100km")&& selectedTo.equals("liters/100 miles")){ 

       fromFuel= Double.parseDouble(etfrom.getText().toString()); 
       resultFuel = fromFuel* 1.61; 
      // String result = String.valueOf(resultFuel); 

       textview.setText("" +resultFuel); 

      }else{ 

      } 
     } 
    }); 

} 


String selectedFrom; 
String selectedTo; 

@Override 
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, 
     long arg3) { 
    // TODO Auto-generated method stub 
    selectedFrom = spinFrom.getItemAtPosition(arg2).toString(); 
    selectedTo = spinTo.getItemAtPosition(arg2).toString(); 

    Toast.makeText(getBaseContext(),"From: " + selectedTo, Toast.LENGTH_LONG).show(); 
} 

@Override 
public void onNothingSelected(AdapterView<?> arg0) { 
    // TODO Auto-generated method stub 

} 

}

+0

什麼'selectedFrom'和'selectedTo'? – nikis

回答

1

請嘗試:

textview.setText(Double.toString(resultFuel)); 
+0

它沒有工作,我只是沒有得到任何東西在屏幕上。 – user3460769

+0

你確定它的內部條件如果條件....也如果你的文本視圖可見? – Kakarot