1

我有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 获取数据,但它不显示值是maleor female

它显示的值为 jame.test.ClassGender@5355eddc 和 jame.test.ClassGender@5355ee54。

如何解决?

4

1 回答 1

0

如果你会看到 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() 方法。因此,您不需要传递 a ,而是List需要ClassGender传递 a ListString否则您可以做的是toString()在您的类中覆盖,ClassGender这将返回性对象值。

于 2016-07-05T04:43:58.617 回答