2010-05-05 127 views
2

我刚刚得到了一个Droid,使用它一段时间后,我觉得我想为它做一个程序。我正在尝试制作的程序计算辅助存储介质的实际存储容量。用户从KB到YB范围内的单位列表中进行选择,并根据所选单位将输入的大小放入公式中。但是,该程序存在一些问题。从我的测试中,我已经缩小到事实,即用户的选择不是真的从微调获得。我所看到的一切似乎都指向一种与J2SE中的工作方式非常类似的方法,但它什么都不做。我实际上应该如何获得这些数据?获取用户选择并将其转换为字符串[Android]

下面是应用程序的Java源代码:

package com.Actual.android; 

import android.app.Activity; 
import android.os.Bundle; 
import android.widget.*; 
import android.view.*; 
public class ActualStorageActivity extends Activity 
{ 

Spinner selection; /* declare variable, in order to control spinner (ComboBox) */ 
ArrayAdapter adapter; /* declare an array adapter object, in order for spinner to work */ 
EditText size; /* declare variable to control textfield */ 
EditText result; /* declare variable to control textfield */ 
Button calculate; /* declare variable to control button */ 
Storage capacity = new Storage(); /* import custom class for formulas */ 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); // load content from XML 

selection = (Spinner)findViewById(R.id.spinner); 
adapter = ArrayAdapter.createFromResource(this, R.array.choices_array, android.R.layout.simple_spinner_dropdown_item); 
size = (EditText)findViewById(R.id.size); 
result = (EditText)findViewById(R.id.result); 
calculate = (Button)findViewById(R.id.submit); 

adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); /* set resource for dropdown */ 

selection.setAdapter(adapter); // attach adapter to spinner 

result.setEnabled(false); // make read-only 
result.setText("usable storage"); 

} 

public void calcAction(View view) { 
    String initial = size.getText().toString(); 
    String unit = selection.getSelectedItem().toString(); 
    String end = "Nothing"; 
    double convert = Double.parseDouble(initial); 
    capacity.setStorage(convert); 

    if (unit == "KB") 
    { 
    end = Double.toString(capacity.getKB()); 
    } 

    else if (unit == "MB") 
    { 
    end = Double.toString(capacity.getMB()); 

    } 

    else if (unit == "GB") 
    { 
    end = Double.toString(capacity.getGB()); 

    } 

    else if (unit == "TB") 
    { 
    end = Double.toString(capacity.getTB()); 

    } 

    else if (unit == "PB") 
    { 
    end = Double.toString(capacity.getPB()); 

    } 

    else if (unit == "EB") 
    { 
    end = Double.toString(capacity.getEB()); 

    } 

    else if (unit == "ZB") 
    { 
    end = Double.toString(capacity.getZB()); 

    } 

    else if (unit == "YB") 
    { 
    end = Double.toString(capacity.getYB()); 

    } 

    else; 

    result.setText(end); 
    } 

} 
+0

这是非常不可读的,请确保您在帖子中突出显示您的代码并单击代码图标。也尽量简化你的问题,太久了! – 2010-05-05 19:32:00

+0

你走了,好多了;) – 2010-05-05 20:09:46

回答

2

,当你想通过值来比较的对象,您应该使用equals方法。 object1 == object2如果object1object2引用同一个对象,则为true。

if (unit.equals("KB"))

甚至更​​好:

if ("KB".equals(unit))

,以避免万一unit一个NullPointerException正好是空

+0

谢谢,真的有帮助。 – Kira 2010-05-05 20:23:05

1

我已经要疯了只是想找到最基础从微调器获取选定值并将其转换为字符串的方式。这是我能做到的唯一方法。

final Spinner spinObj = (Spinner) findViewById(R.id.spin_genre); 
    TextView selection = (TextView)spinObj.getSelectedView(); 
     CharSequence strGenre = selection.getText(); 
相关问题