Spinner 实现key value 效果

  在使用Spinner进行下拉列表时,我们一般都会使用字符串数组的方式加ArrayAdapter,取到的列表值就是我们所看到的Text。如果我们想实现网页中select <option value=""></option>这种效果,看到的和取到的值是不一样的怎么办?

方法有好几种,以下是最简单的方法:

1:首先定义一个类:ZxType.java

public class ZxType {

    public String key;    //用于显示

    public String value;//用于数据操作

    

    public ZxType(String key,String value){

        this.key = key;

        this.value = value;

    }

    

    public String toString(){

        return key;

    }

}

2:ZxzxActivity.java

public class ZxzxActivity extends Activity {

    private Spinner spZxzxType = null;

    private ArrayAdapter<ZxType> adapter = null;

    private ZxType zxTypeArr[] = null;

    

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_zxzx);

        

        spZxzxType = (Spinner)findViewById(R.id.spZxzxType);

        

        getZxTypeArr();

        

        adapter = new ArrayAdapter<ZxType>(

                this,

                android.R.layout.simple_spinner_item,

                zxTypeArr

            );

        

        spZxzxType.setAdapter(adapter);

        

        spZxzxType.setOnItemSelectedListener(listener);

    }

    private OnItemSelectedListener listener = new OnItemSelectedListener(){

        @Override

        public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,

                long arg3) {

            ZxType zxtype = (ZxType)spZxzxType.getSelectedItem();

            String value = zxtype.value;

            Toast.makeText(ZxzxActivity.this, value, Toast.LENGTH_SHORT).show();

        }

        @Override

        public void onNothingSelected(AdapterView<?> arg0) {

            

        }

    };

    

    

    private void getZxTypeArr(){

        zxTypeArr = new ZxType[]{

                new ZxType("咨询类别",""),

                new ZxType("重点税源模块","cc9890ee22ceff760122df0473210110"),

                new ZxType("普通发票类","4028801b1c0bfe55011c0bfe89300002"),

                new ZxType("专用发票类","4028801b1c0bfe55011c0bfea3310003"),

                new ZxType("个体税收类","4028801b1c0bfe55011c0bfecc940004"),

                new ZxType("税务登记类","4028801b1c0bfe55011c0bfeea2f0005"),

                new ZxType("涉税资料填写类","4028801b1c0bfe55011c0bfeea2f0006"),

                new ZxType("增值税类","4028801b1c0bfe55011c0bfeea2f0007"),

                new ZxType("消费税类","4028801b1c0bfe55011c0bfeea2f0008"),

                new ZxType("所得税类","4028801b1c0bfe55011c0bfeea2f0009"),

                new ZxType("外商投资企业所得税类","4028801b1c0bfe55011c0bfeea2f0010"),

                new ZxType("进出口税收类","4028801b1c0bfe55011c0bfeea2f0011"),

                new ZxType("减免税类","4028801b1c0bfe55011c0bfeea2f0012"),

                new ZxType("税收法规类","4028801b1c0bfe55011c0bfeea2f0013"),

                new ZxType("其他类","4028801b1c0bfe55011c0bfeea2f0014"),

                new ZxType("纳税申报类","4028801b1c0bfe55011c0bfeea2f0015"),

                new ZxType("车辆购置税","4028801b1c0bfe55011c0bfeea2f0016"),

                new ZxType("利息税","4028801b1c0bfe55011c0bfeea2f0017")

        };

    }}

3:activity_zxzx.xml

<LinearLayout 

        android:layout_marginTop="8dp"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:orientation="horizontal">

        <TextView 

            android:layout_width="50dp"

            android:layout_height="wrap_content"

            android:text="类别"/>

        <Spinner 

            android:id="@+id/spZxzxType"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:background="@drawable/corner_edit"/>

    </LinearLayout>

 

你可能感兴趣的:(Spinner)