获取用户选择并将其转换为String [Android]

时间:2010-05-05 19:19:34

标签: android spinner

我刚买了一台机器人,在使用它一段时间之后,我觉得我想为它做一个程序。我试图制作的程序计算二级存储介质的实际存储容量。用户从KB到YB范围内的单位列表中进行选择,并根据所选单位将输入的大小放入公式中。但是,该程序存在一些问题。从我的测试中,我已经将其缩小到这样一个事实,即用户的选择并非真正从微调器获得。我查看的所有内容似乎都指向了一种与它在J2SE中的工作方式非常类似的方法,但它什么也没做。我实际上应该如何获取这些数据?

以下是该应用的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);
  }

}

2 个答案:

答案 0 :(得分:2)

如果要按值比较对象,则应使用equals方法。如果object1 == object2object1引用同一个对象,则object2为真。

if (unit.equals("KB"))

甚至更好:

if ("KB".equals(unit))

以避免在unit碰巧为空的情况下出现NullPointerException

答案 1 :(得分:1)

我一直在努力寻找从微调器中获取所选值并将其转换为字符串的最基本方法。这是我能做到的唯一方法。

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