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