0

i have a spinner with 3 options to chose, when the user selects one of the option, the app will perform an operation depending on the option selected, but so far, i can get the value of the spinner selected, i tried 2 forms but the same result came.

The variable pos is supose to get the position value of the spinner.

Once i get the value of the spinner i need to use it on this.

   package com.example.marius.convertidor;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import static android.R.layout.simple_spinner_dropdown_item;


public class MainActivity extends ActionBarActivity implements AdapterView.OnItemSelectedListener {

    Spinner spinner;
    int pos=2;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        spinner = (Spinner) findViewById(R.id.spinner);
        ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.conversiones, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }



    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        pos = position;


    }


    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }


    private void displayMessage(String message) {
        TextView priceTextView = (TextView) findViewById(R.id.val1);
        priceTextView.setText(message);
    }


    public void result(View view) {
        EditText et1 = (EditText) findViewById(R.id.insval);
        String ed1, priceMessage;

        int re1;
        ed1 = et1.getText().toString();

        re1 = Integer.parseInt(ed1);


        double km, m, cm;

        if (pos == 0) {
            km = re1 / 1000;
            priceMessage = "conversion=" + km;
            displayMessage(priceMessage);
        }
        if  (pos == 1) {
            m = re1 * 1000;
            priceMessage = "conversion=" + m;
            displayMessage(priceMessage);
        }
        if  (pos == 2) {
            cm = re1 * 10;
            priceMessage = "conversion=" + cm;
            displayMessage(priceMessage);
        }




    }

}

i hope you can help me with this, i'm sure i'm make some grammar mistakes, i hope you get the idea

3 Answers3

0

This is a sample which prints the selected item value :

Main.java

public class Main extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final Integer items[] = new Integer[4];
    items[0] = 10;
    items[1] = 11;
    items[2] = 12;
    items[3] = 13;

    ArrayAdapter<Integer> adapter =
            new ArrayAdapter<Integer>(
                    this,
                    android.R.layout.simple_spinner_item,
                    items );

    Spinner spinner = (Spinner)findViewById(R.id.spinner_1);

    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);
    spinner.setOnItemSelectedListener(
            new AdapterView.OnItemSelectedListener() {
                public void onItemSelected( AdapterView<?> parent, View view, int position,  long id) {
                    Integer d = (Integer) parent.getItemAtPosition(position);
                    Toast.makeText( Main.this, d.toString(), Toast.LENGTH_SHORT).show();
                }

                public void onNothingSelected(AdapterView<?> parent) {

                }
            }
    );

}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".Main">

<Spinner
    android:layout_width="fill_parent"
    android:layout_marginLeft="5dp"
    android:layout_height="wrap_content"
    android:id="@+id/spinner_1"
    android:gravity="center"
    >
</Spinner>

Mohammad Hammadi
  • 733
  • 2
  • 11
  • 34
0

Since you have array with you which is being used to populate the Spinner drop down, why don't you just use the position you get after an item is selected and use that posiiton to references the array for actual item?

Access strings.xml array in java like this:

String[] spinnerArray = getResources().getStringArray(R.array.conversiones);

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    pos = position;
    // Get String array R.array.conversiones and Use position to get actual item 
    String selectedItem = spinnerArray[position];

}
Green goblin
  • 9,898
  • 13
  • 71
  • 100
0

this is the code i found and do the trick, i has some differences, but its almost the same, i don't get why here pos can get the value of the spinner and in my code don't.

here AdapterView.OnItemSelectedListener is not called, i dont get why.

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class MainActivity extends Activity {



int pos = 0;
int lastValue = 0;

void showToast(CharSequence msg) {
    Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
}

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Spinner spinner = (Spinner)findViewById(R.id.spinner);
    Button btnConvert = (Button)findViewById(R.id.btnConvert);
    final EditText txtEntry = (EditText)findViewById(R.id.insval);

    ArrayAdapter<CharSequence> adapter =ArrayAdapter.createFromResource(
            this, R.array.conversiones, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);


    spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
        @Override
        public void onItemSelected(
                AdapterView<?> parent, View view, int position, long id) {
            //showToast("Spinner2: position=" + position + " id=" + id);
            pos = position;
        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub

        }
    });

    btnConvert.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            // TODO Auto-generated method stub
            if (txtEntry.getText().toString().trim().length() > 0){
                String textValue = txtEntry.getText().toString();
                lastValue = Integer.parseInt(textValue);
                double km, m, cm;

                if(pos==0){
                    km = lastValue / 1000;
                    showToast(lastValue + " m = " +  km + " km(s)");
                }else if(pos==1){
                    m = lastValue * 1000;
                    showToast(lastValue + " km(s) = " +  m + " m");
                }else{
                    cm = lastValue * 100;
                    showToast(lastValue + " m = " +  cm + " cm(s)");
                }
            }
            else{
                showToast("Please Enter Value");

            }

        }
    });
}

}