Call counter value? Also use other values ​​based on the value of the counter.

I have a spinner 'aperture' configured with a list of numbers, and a spinner 'mode' with two options. When the button is pressed, I need a calculation to start using various inputs, including the current selection from the “aperture” and the value obtained from the “mode”. How can I name the value of the counter so that I can use it in the calculation?

Also, how do I use the spinner mode selection to set this other value before I enter it into the calculation? To be more specific, if Small is selected for the counter, then the value that I use in the calculation is 0.015, while if Large is selected, I need to use 0.028

Other inputs are EditText views, so right now I am configured as follows:

    input = (EditText) findViewById(R.id.input1); 
    input2 = (EditText) findViewById(R.id.input2);
    input3 = (EditText) findViewById(R.id.input3); 
    input4 = (EditText) findViewById(R.id.input4);
    output = (TextView) findViewById(R.id.result); 
    output2 = (TextView) findViewById(R.id.result2);

    //aperture dropdown
    Spinner s = (Spinner) findViewById(R.id.apt);
    ArrayAdapter adapter2 = ArrayAdapter.createFromResource(        this, R.array.apertures,       
    android.R.layout.simple_spinner_item);
    adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    s.setAdapter(adapter2);

    //button
    final Button button = (Button) findViewById(R.id.calculate);
    button.setOnClickListener(new View.OnClickListener() {             
     public void onClick(View v) {                 
      // Perform action on click
      doCalculation();
       }         
      });
     }

private void doCalculation() { 
    // Get entered input value 
    String strValue = input.getText().toString(); 
    String strValue2 = input2.getText().toString();
    String strValue3 = input3.getText().toString(); 
    String strValue4 = input4.getText().toString();

    // Perform a hard-coded calculation 
    double imperial1 = (Double.parseDouble(strValue3) + (Double.parseDouble(strValue4) / 12));
    double number = Integer.parseInt(strValue) * 2; 
    double number2 = ((number / 20) + 3) / Integer.parseInt(strValue2);

    // Update the UI with the result 
    output.setText("Result:  "+ number); 
    output2.setText("Result2: "+ imperial1); 
}
}

This is not an actual equation, it is just a test to make sure everything is connected correctly. As I would say the value of the "aperture" of the counter and the mode of "Small / Large counter"

+3
source share
3 answers

To get the counter value, do one of the following:

Object item = spinner.getSelectedItem();

long id = spinner.getSelectedItemId();

int position = getSelectedItemPosition();

View selectedView = getSelectedView();

In your case, you can declare the counter as final and pass the selected item to the doCalculations () method as follows:

    final Spinner aptSpinner = (Spinner) findViewById(R.id.apt);
    ...

    //button
    final Button button = (Button) findViewById(R.id.calculate);
    button.setOnClickListener(new View.OnClickListener() {             
     public void onClick(View v) {                 
      // Perform action on click
      doCalculation(aptSpinner.getSelectedItem());
       }         
      });
     }

    private void doCalculation(Object selectedItem) { 

        ...

    }

You will probably find it helpful to compile the Java base book and find out how the scope works in Java.

+4
source

ArrayAdapter . ( ), . - Java, , spinner. , "", :

com.katr.spinnerdemo;

public class State {

// Okay, full acknowledgment that public members are not a good idea, however
// this is a Spinner demo not an exercise in java best practices.
public int id = 0;
public String name = "";
public String abbrev = "";

// A simple constructor for populating our member variables for this tutorial.
public State( int _id, String _name, String _abbrev )
{
    id = _id;
    name = _name;
    abbrev = _abbrev;
}

// The toString method is extremely important to making this class work with a Spinner
// (or ListView) object because this is the method called when it is trying to represent
// this object within the control.  If you do not have a toString() method, you WILL
// get an exception.
public String toString()
{
    return( name + " (" + abbrev + ")" );
}
}

:

       // Step 1: Locate our spinner control and save it to the class for convenience
    //         You could get it every time, I'm just being lazy...   :-)
    spinner = (Spinner)this.findViewById(R.id.Spinner01);

    // Step 2: Create and fill an ArrayAdapter with a bunch of "State" objects
    ArrayAdapter spinnerArrayAdapter = new ArrayAdapter(this,
          android.R.layout.simple_spinner_item, new State[] {   
                new State( 1, "Minnesota", "MN" ), 
                new State( 99, "Wisconsin", "WI" ), 
                new State( 53, "Utah", "UT" ), 
                new State( 153, "Texas", "TX" ) 
                });

    // Step 3: Tell the spinner about our adapter
    spinner.setAdapter(spinnerArrayAdapter);  

:

State st = (State)spinner.getSelectedItem();

bonafied java . , OnItemSelectedListener .

public void onItemSelected(AdapterView<?> parent, View view, int position, long id) 
{
    // Get the currently selected State object from the spinner
    State st = (State)spinner.getSelectedItem();

    // Now do something with it.
} 

public void onNothingSelected(AdapterView<?> parent ) 
{ 
}

: http://www.katr.com/article_android_spinner01.php

+2

If R.array.apertures is a String Array, you can use

Object item = spinner.getSelectedItem(); 

if(item != null) {

String SelectedString = item.toString();

}

If any other object says Person.

then

if(item != null) {

Person SelectedString = (Person) item;

}
0
source

Source: https://habr.com/ru/post/1729844/


All Articles