无法将数据从xml文件正确加载到Spinner

时间:2016-07-04 08:33:58

标签: android android-spinner android-xml

我有listgender.xml这样的文件:

<gender>
    <sex>male</sex>
    <sex>female</sex>
</gender>

对于微调器,这是spinner_data.xml

<TextView android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/tv_spinner"
    android:textColor="#ff0004"
    android:textSize="14sp"
    xmlns:android="http://schemas.android.com/apk/res/android" />

这是ClassGender

public class ClassGender {
    private String sex;
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
}

这是Activity hold spinner:

Spinner sp;
Button bt;
TextView tv;

static final String KEY_SEX = "sex";

List<ClassGender> spList = null;

String sex;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_4);

    sp = (Spinner) findViewById(R.id.spinner_t4);
    try {
        XmlPullParserSpinner parser_Emp = new XmlPullParserSpinner();
        spList = parser_Emp.parse(getAssets().open("listgender.xml"));

        ArrayAdapter<ClassGender> adapter = new ArrayAdapter<ClassGender>(this,
                R.layout.spinner_data, spList);
        sp.setAdapter(adapter);

        tv = (TextView)findViewById(R.id.tv_T4);
        bt = (Button)findViewById(R.id.bt_T4);
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String text = sp.getSelectedItem().toString();
                tv.setText(text);
            }
        });

    }
    catch (Exception e){
        e.printStackTrace();
    }

}

public class XmlPullParserSpinner {

    private ClassGender c_g;
    private String text;

    public XmlPullParserSpinner() {
        spList = new ArrayList<ClassGender>();
    }

    public List<ClassGender> parse(InputStream is) {
        XmlPullParserFactory factory = null;
        XmlPullParser parser = null;
        try {
            factory = XmlPullParserFactory.newInstance();
            factory.setNamespaceAware(true);
            parser = factory.newPullParser();
            parser.setInput(is, null);
            int eventType = parser.getEventType();
            while (eventType != XmlPullParser.END_DOCUMENT) {
                String tagname = parser.getName();
                switch (eventType) {
                    case XmlPullParser.START_TAG:
                        if (tagname.equalsIgnoreCase(KEY_SEX)) {
                            c_g = new ClassGender();
                        }
                        break;
                    case XmlPullParser.TEXT:
                        text = parser.getText();
                        break;
                    case XmlPullParser.END_TAG:
                        if (tagname.equalsIgnoreCase(KEY_SEX)) {
                            spList.add(c_g);
                            c_g.setSex(text);
                        }
                        break;
                    default:
                        break;
                }
                eventType = parser.next();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return spList;
    }
}

我的微调器可以从XML获取数据,但不是男性或女性。

它显示noname.test.ClassGender@53531c1fnoname.test.ClassGender@53531d74
如何解决?

1 个答案:

答案 0 :(得分:0)

这就是你得到的垃圾价值。

尝试this方法,而不是通过适配器程序设置值。

在string.xml中定义字符串数组,并在布局文件中将其设置为spinner,如此

<Spinner 
        android:id="@+id/spinner"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:drawSelectorOnTop="true"
        android:entries="@array/array_name"
    />