I trying to get a value off a spinner and then pass it onto another java file. I have a spinner in which has a number of values. I change this value, convert it to a string, then on the click of a button I use and intent to pass this information onto the other java file to do stuff with.
The problem I am having is that once I initially set the spinner value even though I change the spinner the value that is passed on is not changed.
Even if I change the spinner the position value that is passed on to LiquidFlowResults is still possize = 0 as originally assigned.
/** initially setting spinner value */
int possize = 0;
String pipeSizeString = Integer.toString(possize);
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button calculateButton = (Button) findViewById(R.id.button1);
calculateButton.setOnClickListener(mEnableListener);
Spinner spinnerPipeSize = (Spinner) findViewById(R.id.spinnerPipeSize);
ArrayAdapter<CharSequence> adapterpipesize = ArrayAdapter.createFromResource(this, R.array.pipesize_array, android.R.layout.simple_spinner_item);
adapterpipesize.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerPipeSize.setAdapter(adapterpipesize);
spinnerPipeSize.setOnItemSelectedListener(new PipeSizeOnItemSelectedListener());
/** Listening for spinner position */
public class PipeSizeOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent,
View view, int possize, long id) {
String pipeSizeString = Integer.toString(possize);
}
public void onNothingSelected(AdapterView<?> parent) {
// Do nothing.
}
}
/** passing on information on click of button */
private OnClickListener mEnableListener = new OnClickListener() {
public void onClick(View view)
{
Intent intent = new Intent(LiquidFlow.this, LiquidFlowResults.class);
Bundle sizeposition = new Bundle();
sizeposition.putString("pipeSizeStringPositionMoved", pipeSizeString);
intent.putExtras(sizeposition);
startActivity(intent);
}
};
There is no need for PipeSizeOnItemSelectedListener. You can just call getSelectedItemPosition in OnClickListener.