Reputation: 9361
the onItemSelected() method is supposed to return a View as one of it's objects and in this case it is a textView that was verified by getting a description and hash of the object in Logcat, so view really is a TextView. the view returned by the method shown here
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
how can i get the String of text that is stored in that view? for example if you do this,
view.getText();
it is supposed to return the String that is stored in the textView, but does not work
however i have tried many different things like casting view to TextView to get the stored String from view and none of these things worked. one of my failed attempts is like this here
((TextView) view).getText()
how can i get the String from the view that is returned by the onItemSelected callback method?
An ArrayList is loaded with Strings and is put into the adapter shown here,
public class SpinnerAdapter extends ArrayAdapter<String>{
ArrayList<String> objects;
Context context;
int textViewResourceId;
public SpinnerAdapter(Context context, int textViewResourceId, ArrayList<String> objects) {
super(context, textViewResourceId, objects);
this.context = context;
this.textViewResourceId = textViewResourceId;
this.objects = objects;
}
spinnerOne.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
Toast.makeText(StandardSelectionSettingsSmallTank.this, "id returned "+ Long.toString(id) , Toast.LENGTH_SHORT).show();
Toast.makeText(StandardSelectionSettingsSmallTank.this, "view returned "+ ((TextView) view).getText() , Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
}
EDIT: I just tried out the following code and it is doing exactly what I need done. it gets the string that is stored in the current position of the spinner. the string that I loaded earlier with the ArrayList. this is working so i guess i will use this instead of using the View object that was returned by the onItemSelected method.
String selection = spinnerOne.getSelectedItem().toString();
i will use this unless anyone has an idea on how to do it the other way by using the view object
Upvotes: 3
Views: 1406
Reputation: 21117
You can get the string from the object you set in adapter by the int position.
this.objects.get(position).getYourString();
Upvotes: 0
Reputation: 3236
Check This one .
parent.getItemAtPosition(position).toString() in place of `((TextView) view).getText()`
Upvotes: 3