Select Item in Spinner - Android

2019-05-22 10:10发布

问题:

I have an item with id and value and I want to add them into a spinner. So when I select the item, I will get the id of it. I can only add itemValue as below and get the selected String.

Can anyone give me the solution for this?

List<String> list = new ArrayList<String>();
                list.add("item 1");
                list.add("item 2");
                list.add("item 3");
                ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(
                        this, android.R.layout.simple_spinner_item, list);
                dataAdapter
                        .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                final Spinner sp = new Spinner(this);
                sp.setAdapter(dataAdapter);

回答1:

items class

public class items {
private String name;
private int id;

public String getname() {
    return name;
}
public void setname(String name) {
    this.name = name;
}

public int getid() {
    return id;
}
public void setid(int id) {
    this.id =id;
}
@Override
public String toString() {
    return name;
}
}

now adding items to array list like this

List<items> values1=new ArrayList();
items comment = new items();
    comment.setname("name1");
    comment.setid("id1");
values1.add(comment);
ArrayAdapter<items> dataAdapter = new ArrayAdapter<String>(
                    this, android.R.layout.simple_spinner_item, list);...

as you did in your code from here.. set onitemselected listener like this...

sp.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
items item1 = (items)arg0.getItemAtPosition(arg2);
int id= item1.id<---- get id here..


回答2:

Make a pojo class of your own say Item and add two fields in it, id and name.
Then make a list of those items and write your own adapter for the spinner, and use it.
It will return you the whole object of the Item class when you click any item of it.

Else, if the item ids are the sequential then you can map them with the item ids too and can carry on with the same implementation, you have done right now.

But first approach is the recommended one, as you are developing using an Object oriented language and your data structures must represent the actual objects of your requirement in the application.



回答3:

public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
    // TODO Auto-generated method stub


    //code one
    String arr[]=getResources().getStringArray(R.array.days);

    text1.setText(arr[arg2]);

    //or 

    //code two
    text1.setText(((TextView)arg1).getText());
}


回答4:

Create new enum:

public enum EnumerateThis {
    ENUM1(0, "Use this like a value"),
    ENUM2(1, "and first argumenta like an integer id");

    private Integer id;
    private String descr;

    private EnumerateThis (Integer id, String place){
        this.id = id;
        this.descr = place;
    }

    public Integer getId() {
        return id;
    }

    public String getDescr() {
        return descr;
    }
}

than using spinner adapter:

ArrayAdapter<EnumerateThis> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, EnumerateThis.values());