我有listgender.xml
这样的资产文件夹中的存储:
<gender>
<sex>male</sex>
<sex>female</sex>
</gender>
这是班级性别:
public class ClassGender {
private String sex;
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
这是list_data
列表视图:
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv_gender"
android:textColor="#ff0004"
android:textSize="14sp" />
我使用 XmlPullParser 解析 xml 到 Listview:
ListView lv;
static final String KEY_GENDER = "sex";
List<ClassGender> spList = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_t4);
lv = (ListView) findViewById(R.id.listview_t4);
try {
XmlPullParserSpinner parser_Emp = new XmlPullParserSpinner();
spList = parser_Emp.parse(getAssets().open("listgender.xml"));
ArrayAdapter<ClassGender> adapter = new ArrayAdapter<ClassGender>(this,
R.layout.list_data, spList);
lv.setAdapter(adapter);
}
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_GENDER)) {
c_g = new ClassGender();
}
break;
case XmlPullParser.TEXT:
text = parser.getText();
break;
case XmlPullParser.END_TAG:
if (tagname.equalsIgnoreCase(KEY_GENDER)) {
spList.add(c_g);
c_g.setSex(text);
}
break;
default:
break;
}
eventType = parser.next();
}
} catch (Exception e) {
e.printStackTrace();
}
return spList;
}
}
我的 listView 可以从 xml 获取数据,但它不显示值是male
or female
,
它显示的值为 jame.test.ClassGender@5355eddc 和 jame.test.ClassGender@5355ee54。
如何解决?