开发者

How to access position variable in onItemSelected()

In the following example, how would I access the "position" variable from outside the parent class?

void onItemSelected(AdapterView<?开发者_Go百科> parent, View view, int position, long id) {
    Country country = (Country) parent.getAdapter().getItem(position);
    spinner2.setAdapter(new ArrayAdapter(country.getStates());
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜