划分两个微调值

时间:2013-08-07 17:46:43

标签: java android spinner

我设法获得了在文本框上显示的微调器值,但我需要将值相互划分。所以我必须将微调器1的值除以微调器2的值。但我不确定如何做到这一点。

这是我的代码:

public class CurrencyActivity extends Activity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.currency);


 final Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
 final Spinner spinner2 = (Spinner) findViewById(R.id.spinner2);
 final EditText etResult = (EditText) findViewById(R.id.etResult);
 final MyData items[] = new MyData[3];
 items[0] = new MyData("EUR, Euro","1.00");
 items[1] = new MyData("GBP, British Pound","5.00");
 items[2] = new MyData("USD, US Dollar","15.00");

ArrayAdapter<MyData> adapter = new ArrayAdapter <MyData>(this, android.R.layout.simple_spinner_item, items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter);
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {


    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id){
        MyData d = items[pos];
        etResult.setText(d.getValue());
}

    @Override
    public void onNothingSelected(AdapterView parent){
        //Do nothing

}
});



spinner2.setAdapter(adapter);
spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id){
        MyData d = items[pos];
        etResult.setText(d.getValue());
        }

    @Override
    public void onNothingSelected(AdapterView parent){
        //Do nothing
    }
 });

我创建了一个具有这些方法的MyData类

public class MyData {

    public MyData(String spinnerText, String value){
        this.spinnerText = spinnerText;
        this.value = value;
    }
    public String getSpinnerText(){
        return spinnerText;
    }
    public String getValue(){
        return value;
    }
    public String toString(){
        return spinnerText;
    }
    String spinnerText;
    String value;

}

谢谢

2 个答案:

答案 0 :(得分:0)

有一个简单的解决方案,但我认为你在这里混合代码。
Integer.parseInt(d.getValue())会为您提供一个有效的int,您可以执行任何数学函数,但在您的代码中,当每个Spinner选项发生更改时,您会覆盖MyData d
您可以在MyData课程中添加另一个字段,该字段会将您想要的任何值存储为int,然后只需使用mydata.getVal(),假设mydata属于/ {班MyData

答案 1 :(得分:0)

我能够解决问题。

我将MyData类中的值类型从String更改为double。 我在CurrencyActivity中创建了两个名为valA和valB的双字段。

然后在方法中我将微调器的值赋给val字段,例如 valA = d.getValue();

然后我创建了一个新方法,只做了valA / valB;

这是帮助您更容易理解的代码。

public class CurrencyActivity extends Activity {
double valA; 
double valB;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.currency);


final Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
    final Spinner spinner2 = (Spinner) findViewById(R.id.spinner2);
    final EditText etResult = (EditText) findViewById(R.id.etResult);
    final MyData items[] = new MyData[3];
    items[0] = new MyData("EUR, Euro",1);
    items[1] = new MyData("GBP, British Pound",5);
    items[2] = new MyData("USD, US Dollar",15);

ArrayAdapter<MyData> adapter = new ArrayAdapter <MyData>(this,  android.R.layout.simple_spinner_item, items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter);
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {


    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id){
        MyData d = items[pos];
        valA=d.getValue();
}

    @Override
    public void onNothingSelected(AdapterView parent){
        //Do nothing

}
});



spinner2.setAdapter(adapter);
spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id){
        MyData d = items[pos];
        valB = d.getValue();
        }
    @Override
    public void onNothingSelected(AdapterView parent){
        //Do nothing
    }
 });

public void divideVals(){

    double result = valA / valB;
    etResult.setText(String.valueOf(result));
}

希望这对其他人有所帮助,因为我花了很多时间来解决这个问题,最终这个问题非常简单。