Setting ID for Spinner items

六眼飞鱼酱① 提交于 2019-12-18 11:10:18

问题


I have an array of Strings I'm populating a Spinner object with. However, I'd like to attach an ID to each element of the Spinner, so when the user selects an item, I have its ID to use to save to some other piece of data. How can I do this?


回答1:


What do you mean by id. You can use ArrayAdapter to populate the Spinner. When item is selected just get the element from the adapter and save the data you want.

Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<MyObject> adapter = ... // initialize the adapter
adapter.setDropDownViewResource(android.R.layout.some_view);
spinner.setAdapter(adapter);

and when item is selected

public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
    MyObject selected = parent.getItemAtPosition(pos);
    // save any data relevant with selected item   
}

If you are storing your data in db you can use CursorAdapter and in onItemSelected to fetch the selected item id from the cursor.




回答2:


Create a class StringWithTag and use in place of the string name in the list like so :-

public class StringWithTag {
    public String string;
    public Object tag;

    public StringWithTag(String stringPart, Object tagPart) {
        string = stringPart;
        tag = tagPart;
    }

    @Override
    public String toString() {
        return string;
    }
}

in the add items to spinner part :-

List<StringWithTag> list = new ArrayList<StringWithTag>();
list.add(new StringWithTag("Oldman", "12345"));
list.add(new StringWithTag("Umpire", "987654"));
list.add(new StringWithTag("Squad", "ABCDEE"));
ArrayAdapter<StringWithTag> adap = new ArrayAdapter<StringWithTag> (this, android.R.layout.simple_spinner_item, list);
....
....

in the listener :-

public void onItemSelected(AdapterView<?> parant, View v, int pos, long id) {
    StringWithTag s = (StringWithTag) parant.getItemAtPosition(pos);
    Object tag = s.tag;
}

voila! }




回答3:


I don't think you can attach an arbitrary ID to elements of a text array resource, if that's what you're using.

I think the simplest way to attach such an ID would be to either hard-code (if you're using a static text resource) or dynamically build (if you get the strings at runtime) a mapping from (String position in array)->(primary key).

EDIT: On the other hand, Mojo Risin has a point - you should check to see if the CursorAdapter API already does what you need for you.




回答4:


Andrew Hi, it's been a long time but it's worth to write.

You can set a tag for each row when you'r inflating spinnerLayout in SpinnerAdapter:

spinnerView = inflater.inflate(spinnerLayout, parent, false);
spinnerView.setTag("Your Tag");

And then you can get the tag with:

yourSpinner.getSelectedView().getTag();



回答5:


I think The best solution is to add one more spinner and fill it with the ids but make the visibility of it to gone



来源:https://stackoverflow.com/questions/5792159/setting-id-for-spinner-items

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!