无法在TextView中获得双输出

时间:2014-05-05 22:29:36

标签: java android parsing textview android-spinner

此Android应用程序的主要目的是学习如何使用微调器和解析数据。我尝试过很多东西,但仍然无法正常工作。我无法在屏幕上获得resultFuel的输出。

这是我MainActivity中的代码:

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){

    }
    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

}

}

1 个答案:

答案 0 :(得分:1)

请尝试:

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