Android Spinner is working -- but can't parse and pass the selected value
I'm trying to pass a value from an android spinner selection into a url. All of my other vars are passing and the toast on the spinner in displaying the correct spinner choice when selected. (for purposes here, the code doesn't show all vars.)
My log shows the country VAR as NULL. Need to get the "country0" value to pass like the others. How can I make this happen?
thanks
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(DEBUG_TAG, "onCreate");
mContext = this; //TODO legacy may not be needed
SERVER = this.getString(R.string.mygallerist_server_base);
settings = getSharedPreferences(PREFS_NAME, 0);
uid = settings.getString("uid", NO开发者_如何学JAVAT_SET);
//SPINNER
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.countries_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
mOver35CheckBox = (CheckBox) findViewById(R.id.Over35);
class MyOnItemSelectedListener implements OnItemSelectedListener {
//SPINNER PARSING
@Override
public void onItemSelected(AdapterView<?> country0,
View view, int pos, long id) {
Toast.makeText(country0.getContext(), "The country is " +
country0.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView country0 ) {
// Do nothing.
}
}
spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
final String country = country0;///MAYBE THIS IS WHAT IS WRONG
setPassword.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
// If all fields filled in then go to server
String country1 = country; /// OR MAYBE THIS IS WHAT IS WRONG
String userName = mUserNameEditText.getText().toString();
Perhaps I'm missing something in your question, but when you select the value for the Toast, you could just set the value of a private member, then read this value in your onClick, (being as you know you're getting the correct value in the onItemSelected)
private String countrySelection;
public void onItemSelected(AdapterView<?> country0, View view, int pos, long id) {
countrySelection = country0.getItemAtPosition(pos).toString();
Toast.makeText(country0.getContext(), "The country is " + countrySelection, Toast.LENGTH_LONG).show();
}
...
final String country = countrySelection;
setPassword.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
sendDataToServer(country, ...);
Does this solve your issue?
Edit...
What type is country0 in this context? (I don't have comment privileges yet)
final String country = country0;///MAYBE THIS IS WHAT IS WRONG
精彩评论