Reputation: 43
I have created a spinner with 3 options: Beginner, Advanced and Pro. When one of the options is selected, I want to create a toast and set a double. I need the double to use that value later on, to calculate with 2 other values from an edittext. For developing purposes I want to set a textview with that value, to check it. I have tried a few things, but I cant bring it work. The textview keeps displaying 0.0
Hope someone can help me.
My java code:
package tim.steunebrink.tab;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
public class AlbumsActivity extends Activity{
private TextView ExpValue;
private double expchosen;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.albums_layout);
ExpValue = (TextView) findViewById(R.id.ExpValue);
Spinner spinner = (Spinner) findViewById(R.id.spinner_experience);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.experience_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
class MyOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
if (pos == 0) {
// Beginner
Toast.makeText(parent.getContext(), "Beginner = 0.5", Toast.LENGTH_LONG).show();
double expchosen = new Double("0.5");
} else if(pos == 1) {
// Advanced
Toast.makeText(parent.getContext(), "Advanced = 1.0", Toast.LENGTH_LONG).show();
double expchosen = new Double("1.0");
} else {
// Pro
Toast.makeText(parent.getContext(), "Pro = 1.5", Toast.LENGTH_LONG).show();
double expchosen = new Double("1.5");
}
ExpValue.setText(Double.toString(expchosen));
}
public void onNothingSelected(AdapterView<?> parent) {
// Do Nothing
}
}
spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
}
}
Upvotes: 0
Views: 1643
Reputation: 2075
Make these Changes
class MyOnItemSelectedListener implements OnItemSelectedListener {
double expchosen ;
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
if (pos == 0) {
// Beginner
Toast.makeText(parent.getContext(), "Beginner = 0.5", Toast.LENGTH_LONG).show();
expchosen= new Double("0.5");
} else if(pos == 1) {
// Advanced
Toast.makeText(parent.getContext(), "Advanced = 1.0", Toast.LENGTH_LONG).show();
expchosen = new Double("1.0");
} else {
// Pro
Toast.makeText(parent.getContext(), "Pro = 1.5", Toast.LENGTH_LONG).show();
expchosen = new Double("1.5");
}
ExpValue.setText(Double.toString(expchosen));
}
public void onNothingSelected(AdapterView<?> parent) {
// Do Nothing
}
}
Upvotes: 2
Reputation: 2024
Below code may be useful to you.
Apply
ExpValue.invalidate();
After line
ExpValue.setText(Double.toString(expchosen));
Upvotes: 1