2010-05-05 129 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(); 
相關問題