1

i have listgender.xml store in asset folder like this:

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

this is class gender :

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

this is list_data for listview:

<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" />

I use XmlPullParser parse xml to 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;
    }
}

my listView can get data from xml, but it not show value is male or female,

it show value is jame.test.ClassGender@5355eddc and jame.test.ClassGender@5355ee54.

how to fix it ?

2
  • 1
    It's not answer.just suggestion. XML is old age..You should try Json :) Commented Jul 5, 2016 at 4:35
  • Please don't duplicate your own question, instead improve it by adding clarity. Commented Jul 5, 2016 at 12:05

1 Answer 1

0

If you will see the code of 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

}

The line

        text.setText(item.toString());

uses the toString() method of the item. So, instead of passing a List of ClassGender you need to pass a List of String otherwise what you can do is override toString() in your class ClassGender which will return the sex object value.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.