What's the correct way to implement key-value pair in Spinner in android

后端 未结 4 1723
無奈伤痛
無奈伤痛 2020-12-04 14:15

Is this the correct way to implement key-value pair for a Spinner in Android?

package com.mypackage

import android.app.Activity;
import android.os.Bundle;
i         


        
4条回答
  •  眼角桃花
    2020-12-04 15:02

    To avoid reaching back into items[] from the listener use getItemAtPosition which returns an Object from the Adapter. To access the MyData methods you must cast the object like so:

    public void onItemSelected(
            AdapterView parent, View view, int position, long id) {
        MyData d = (MyData) parent.getItemAtPosition(position);
        valueTextView.setText( d.getValue() );
    }
    

提交回复
热议问题