android XmlPullParser 解析 xml 到 Listview
android XmlPullParser parse xml to Listview
我 listgender.xml
像这样存储在资产文件夹中:
<gender>
<sex>male</sex>
<sex>female</sex>
</gender>
这是 class 性别:
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
或 female
、
it show value is jame.test.ClassGender@5355eddc and
jame.test.ClassGender@5355ee54.
如何解决?
如果你会看到ArrayAdapter的代码
public class ArrayAdapter<T> extends BaseAdapter implements Filterable, ThemedSpinnerAdapter {
//Some code
public View getView(int position, View convertView, ViewGroup parent) {
return createViewFromResource(mInflater, position, convertView, parent, mResource);
}
private View createViewFromResource(LayoutInflater inflater, int position, View convertView,
ViewGroup parent, int resource) {
View view;
TextView text;
if (convertView == null) {
view = inflater.inflate(resource, parent, false);
} else {
view = convertView;
}
try {
if (mFieldId == 0) {
// If no custom field is assigned, assume the whole resource is a TextView
text = (TextView) view;
} else {
// Otherwise, find the TextView field within the layout
text = (TextView) view.findViewById(mFieldId);
}
} catch (ClassCastException e) {
Log.e("ArrayAdapter", "You must supply a resource ID for a TextView");
throw new IllegalStateException(
"ArrayAdapter requires the resource ID to be a TextView", e);
}
T item = getItem(position);
if (item instanceof CharSequence) {
text.setText((CharSequence)item);
} else {
text.setText(item.toString());
}
return view;
}
//Some code
}
行
text.setText(item.toString());
使用项目的 toString() 方法。因此,您需要传递 String
的 List
而不是传递 ClassGender
的 List
,否则您可以做的是在 [=24] 中覆盖 toString()
=] ClassGender
这将 return 性对象值。
我 listgender.xml
像这样存储在资产文件夹中:
<gender>
<sex>male</sex>
<sex>female</sex>
</gender>
这是 class 性别:
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
或 female
、
it show value is jame.test.ClassGender@5355eddc and jame.test.ClassGender@5355ee54.
如何解决?
如果你会看到ArrayAdapter的代码
public class ArrayAdapter<T> extends BaseAdapter implements Filterable, ThemedSpinnerAdapter {
//Some code
public View getView(int position, View convertView, ViewGroup parent) {
return createViewFromResource(mInflater, position, convertView, parent, mResource);
}
private View createViewFromResource(LayoutInflater inflater, int position, View convertView,
ViewGroup parent, int resource) {
View view;
TextView text;
if (convertView == null) {
view = inflater.inflate(resource, parent, false);
} else {
view = convertView;
}
try {
if (mFieldId == 0) {
// If no custom field is assigned, assume the whole resource is a TextView
text = (TextView) view;
} else {
// Otherwise, find the TextView field within the layout
text = (TextView) view.findViewById(mFieldId);
}
} catch (ClassCastException e) {
Log.e("ArrayAdapter", "You must supply a resource ID for a TextView");
throw new IllegalStateException(
"ArrayAdapter requires the resource ID to be a TextView", e);
}
T item = getItem(position);
if (item instanceof CharSequence) {
text.setText((CharSequence)item);
} else {
text.setText(item.toString());
}
return view;
}
//Some code
}
行
text.setText(item.toString());
使用项目的 toString() 方法。因此,您需要传递 String
的 List
而不是传递 ClassGender
的 List
,否则您可以做的是在 [=24] 中覆盖 toString()
=] ClassGender
这将 return 性对象值。