tags:

views:

10

answers:

1

In the following example, How would one access "position" variable from elsewhere (outside the parent class) :

void onItemSelected(AdapterView parent, View view, int position, long id) { Country country = (Country) parent.getAdapter().getItem(position); spinner2.setAdapter(new ArrayAdapter(country.getStates()); }

Thanks.

A: 

You could pass the position value outside, but you also have to know something about the adapter and the data source of the adapter. If your adapter is using an array for the data source, position is usually the position of the item in the array. So if you have some other access point to the array, the position would be that element. If the data source of the adapter is a Cursor, position won't help you much outside because you can't be sure of which record the Cursor was pointing to at that position. So unfortunately, the answer is "it depends". Using id is better if the data source is a content provider since you can append the id to the base Uri and access the record that way outside of this callback.

Dave MacLean