开发者

get user selection and convert it to a String [Android]

I just got a Droid, and after having used it for a while, I felt like I wanted to make a program for it. The program that I am trying to make calculates the actual storage capacity of secondary storage mediums. The user select from a list of units that ranges from KB to YB and the size the entered gets put into a formula depending on the chosen unit. However, there is a bit of a problem with the program. From my testing, I have narrowed it down to the fact that the user's selection is not really being obtained from the spinner. Everything I look up seems to point me to a method quite similar to how it works in J2SE, but it does nothing. How am I actually supposed to get that data?

Here is the Java source code for the app:

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);
  }

}


You should use the equals method when you want to compare objects by value. object1 == object2 is true if object1 and object2 refer to the same object.

if (unit.equals("KB"))

or even better:

if ("KB".equals(unit))

to avoid a NullPointerException in case unit happened to be null


I've been going nuts just trying to find the most basic way to get the selected value from a spinner and converting it to a string. Here's the only way I could do it.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜