want to print group name and its child data
here
i am trying to print dynamically particular group name and its child data like header and its contents .
but i m able to pring group name in header that is no issue.
But the problem is, i m also getting the group name instead of child contents
link to my code
here is the code: SectionDemo.java
package com.bestdambikers;
import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class SectionedDemo extends ListActivity {
String strUrl = "http://192.168.5.10/ijoomer_development/index.php?option=com_ijoomer&plg_name=jomsocial&pview=user&ptask=field_list&userid=80&sessionid="+ConstantData.session_id+"&tmpl=component";
DetailBean dBean;
XmlParser parser;
ArrayList<Object> result;
List<DetailBean> list;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.header_main);
//dBean = new DetailBean();
parser = new XmlParser(strUrl, new DetailBean());
result = parser.ParseUrl("data", "group");
int r = result.size();
for(int i=0; i<result.size(); i++)
{
dBean = (DetailBean)result.get(i);
list=Arrays.asList(dBean);
Collections.shuffle(list);
adapter.addSection(dBean.group_name,
new ArrayAdapter<DetailBean>(this,
android.R.layout.simple_list_item_1,
list));
}
setListAdapter(adapter);
}
SectionedAdapter adapter=new SectionedAdapter() {
protected View getHeaderView(String caption, int index,
View convertView,
ViewGroup parent) {
TextView result=(TextView)convertView;
if (convertView==null) {
result=(TextView)getLayoutInflater()
.inflate(R.layout.header,
null);
}
result.setText(caption);
return(result);
}
};
}
and code for DetailBean.java
public class DetailBean
{
public String data = null;
public String code = null;
public String fields = null;
public String group_name = null;
public String field = null;
public String id = null;
public String name = null;
public String value = null;
public String status = null;
public String required = null;
public String type = null;
public DetailBean()
{
this("","","","","","","","","","","");
}
public DetailBean(String data,String code,String fields, String group_name,String field, String id,String name,String value,String status,String required,String type)
{
this.data = data;
this.code = code;
this.fields = fields;
this.group_name = group_name;
this.field = field;
this.id = id;
this.name = name;
this.value = value;
this.status = status;
this.required = required;
this.type = type;
}
@Override
public String toString()
{
// You should populate this string with the data
// you need inside the TextView
return this.group_name + " " + this.data;
}
}
xml file which i want to parse
<data>
<code>1</code>
<fields>
<group>
<group_name>Basic Information</group_name>
<field>
<id>2</id>
<name>Gender</name>
<value>female</value>
<required>1</required>
<type>select</type>
</field>
<field>
<id>3</id>
<name>Birthday</name>
<value>05-06-2011</value>
<required>1</required>
<type>date</type>
</field>
<field>
<id>4</id>
<name>About me</name>
<value>Well meet me u will come to know</value>
<required>1</required>
<type>textarea</type>
</field>
</group>
<group>
<group_name>Contact Information</group_name>
<field>
<id>6</id>
<name>Mobile phone</name>
<value>5555555555</value>
<required>0</required>
<type>text</type>
</field>
<field>
<id>7</id>
<name>Land phone</name>
<value>6666666666</value>
<required>0</required>
<type>text</type>
</field>
<field>
<id>8</id>
<name>Address</name>
<value>Tassel global</value>
<required>1</required>
<type>textarea</type>
</field>
<field>
<id>9</id>
<name>State</name>
<value>Gujarat</value>
<required>1</required>
<type>text</type>
</field>
<field>
<id>10</id>
<name>City / Town</name>
<value>Ahmedabad</value>
<required>1</required>
<type>text</type>
</field>
<field>
<id>11</id>
<name>Country</name>
<value>India</value>
<required>1</required>
<type>country</type>
</field>
<field>
<id>12</id>
<name>Website</name>
<value>http://www.google.com</value>
<required>1</required>
<type>url</type>
</field>
</group>
<group>
<group_name>Education</group_name>
<field>
<id>14</id>
<name>College / University</name>
<value>California university</value>
<required>1</required>
<type>text</type>
</field>
<field>
<id>15</id>
<name>Graduation Year</name>
<value>2010</value>
<required>1</required>
<type>text</type>
</field>
</group>
</fields>
</data>
XmlParser.java
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import android.util.Log;
public class XmlParser extends DefaultHandler
{
public String RootElement;
public String RecordElement;
public InputStream in;
public Object mainObj;
public Object newObj;
public boolean inProcess;
public String xmlURL;
public ArrayList<Object> Records = null;
private final String TAG = "XmlParser";
StringBuffer buffer = new StringBuffer();
String elementName;
String elementValue;
public XmlParser(final InputStream is, final Object tempObj)
{
this.in = is;
this.mainObj = tempObj;
Log.i("Object value", "" + this.mainObj);
this.inProcess = false;
}
public XmlParser(final String strURL, final Object tempObj)
{
this.xmlURL = strURL;
this.mainObj = tempObj;
this.inProcess = false;
}
public ArrayList<Object> ParseUrl(final String rootElement, final String recordElement)
{
this.RootElement = rootElement;
Log.i("RootElement", this.RootElement);
this.RecordElement = recordElement;
Log.i("RecordElement", this.RecordElement);
try
{
final URL sourceUrl = new URL(this.xmlURL);
Log.d("URl", this.xmlURL);
final SAXParserFactory spf = SAXParser开发者_如何学JAVAFactory.newInstance();
final SAXParser sp = spf.newSAXParser();
final XMLReader reader = sp.getXMLReader();
reader.setContentHandler(this);
reader.parse(new InputSource(sourceUrl.openStream()));
}
catch (final Exception e)
{
e.printStackTrace();
return null;
}
return this.Records;
}
public ArrayList<Object> parse(final String rootElement, final String recordElement)
{
this.RootElement = rootElement;
this.RecordElement = recordElement;
Log.i("Root Element", "" + this.RootElement);
Log.i("Record Element", "" + this.RecordElement);
try
{
final SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
parser.parse(this.in, this);
}
catch (final Exception e)
{
e.printStackTrace();
return null;
}
return this.Records;
}
@Override
public void startElement(final String Uri, final String localName,
final String qName, final Attributes attributes)throws SAXException
{
Log.d("URl", this.xmlURL);
this.elementValue = "";
Log.i("IN STARTELEMENT", "" + this.elementValue);
if (localName.length() > 0)
{
Log.i("Local Name Length", "" + localName.length());
Log.i("LocalName", "" + localName);
if (localName.equalsIgnoreCase(this.RootElement))
{
this.Records = new ArrayList<Object>();
Log.i("Root element", "" + this.RootElement);
Log.i("Records", "" + this.Records);
}
else
if (localName.equalsIgnoreCase(this.RecordElement))
{
this.newObj = ClassUtils.newObject(this.mainObj);
Log.i("Main Object", "" + this.mainObj);
Log.i("Record element", "" + this.RecordElement);
ClassUtils.objectMapping(this.newObj, localName, this.elementValue);
Log.i("Element Value", "" + this.elementValue);
this.inProcess = true;
}
}
}
@Override
public void characters(final char[] ch, final int start,
final int length) throws SAXException
{
this.elementValue += new String(ch, start, length).trim();
Log.i("CHARACTERS VALUE", "" + this.elementValue);
}
@Override
public void endElement(final String Uri, final String localName,
final String qName) throws SAXException
{
if (localName.equalsIgnoreCase(this.RecordElement))
{
this.Records.add(this.newObj);
this.inProcess = false;
}
else
if (this.inProcess)
ClassUtils.objectMapping(this.newObj, localName, this.elementValue);
}
}
Your xml suggests that you are dealing with a hierarchical data structure, so my advice would be to use ExpandableListView
with a proper adapter
.
Your bean structure should be rethought as well, to be able to hold hierarchy. For this you should split your DetailBean
class, and leave in that only the groupName
, code
and fields
members:
DetailBean.java
import java.util.ArrayList;
public class DetailBean
{
private String groupName = null;
private int code;
private ArrayList<Field> fields;
public String getGroupName()
{
return groupName;
}
public void setGroupName(String groupName)
{
this.groupName = groupName;
}
public int getCode()
{
return code;
}
public void setCode(int code)
{
this.code = code;
}
public ArrayList<Field> getFields()
{
return fields;
}
public void setFields(ArrayList<Field> fields)
{
this.fields = fields;
}
@Override
public String toString()
{
// You should populate this string with the data
// you need inside the TextView
return this.groupName;
}
}
And declare a new class named Field, to gather the child data:
Field.java
public class Field
{
private int id;
private String name;
private String value;
private boolean required;
private String type;
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getValue()
{
return value;
}
public void setValue(String value)
{
this.value = value;
}
public boolean isRequired()
{
return required;
}
public void setRequired(boolean required)
{
this.required = required;
}
public String getType()
{
return type;
}
public void setType(String type)
{
this.type = type;
}
}
I'd split the XmlParser class too, to have the handler part in a different class:
XmlHandler.java
public class XmlHandler extends DefaultHandler
{
private static final String TAG_GROUP = "group";
private static final String TAG_GROUPNAME = "group_name";
private static final String TAG_FIELD = "field";
private static final String TAG_CODE = "code";
private static final String TAG_ID = "id";
private static final String TAG_NAME = "name";
private static final String TAG_VALUE = "value";
private static final String TAG_REQUIRED = "required";
private static final String TAG_TYPE = "type";
private int code = 0;
private String currentNodeName;
private DetailBean currentGroup;
private Field currentField;
private ArrayList<DetailBean> records = null;
private String elementValue;
public ArrayList<DetailBean> getRecords()
{
return records;
}
@Override
public void startDocument() throws SAXException
{
super.startDocument();
this.records = new ArrayList<DetailBean>();
}
@Override
public void startElement(final String Uri, final String localName, final String qName,
final Attributes attributes) throws SAXException
{
if (localName != null)
{
currentNodeName = localName;
}
}
@Override
public void characters(final char[] ch, final int start, final int length) throws SAXException
{
if (this.currentNodeName == null)
return;
this.elementValue = new String(ch, start, length).trim();
if (this.currentNodeName.equalsIgnoreCase(TAG_CODE))
{
this.code = Integer.parseInt(this.elementValue);
}
else if (this.currentNodeName.equalsIgnoreCase(TAG_GROUP))
{
this.currentGroup = new DetailBean();
this.currentGroup.setCode(this.code);
this.currentGroup.setFields(new ArrayList<Field>());
}
else if (this.currentNodeName.equalsIgnoreCase(TAG_GROUPNAME))
{
this.currentGroup.setGroupName(this.elementValue);
}
else if (this.currentNodeName.equalsIgnoreCase(TAG_FIELD))
{
this.currentField = new Field();
}
else if (this.currentNodeName.equalsIgnoreCase(TAG_ID))
{
this.currentField.setId(Integer.parseInt(this.elementValue));
}
else if (this.currentNodeName.equalsIgnoreCase(TAG_NAME))
{
this.currentField.setName(this.elementValue);
}
else if (this.currentNodeName.equalsIgnoreCase(TAG_VALUE))
{
this.currentField.setValue(this.elementValue);
}
else if (this.currentNodeName.equalsIgnoreCase(TAG_REQUIRED))
{
this.currentField.setRequired(Integer.parseInt(this.elementValue) > 0);
}
else if (this.currentNodeName.equalsIgnoreCase(TAG_TYPE))
{
this.currentField.setType(this.elementValue);
}
}
@Override
public void endElement(final String Uri, final String localName, final String qName) throws SAXException
{
if (localName.equalsIgnoreCase(TAG_GROUP))
{
if (this.currentGroup != null)
this.records.add(this.currentGroup);
}
else if (localName.equalsIgnoreCase(TAG_FIELD))
{
if ((this.currentGroup != null) && (this.currentField != null))
this.currentGroup.getFields().add(this.currentField);
}
currentNodeName = null;
}
}
So your XmlParser
class gets simpler:
XmlParser.java
public class XmlParser
{
public ArrayList<DetailBean> parseFromUrl(final String xmlURL)
{
try
{
final URL sourceUrl = new URL(xmlURL);
Log.d("URL", xmlURL);
final SAXParser sp = SAXParserFactory.newInstance().newSAXParser();
final XMLReader reader = sp.getXMLReader();
final XmlHandler handler = new XmlHandler();
reader.setContentHandler(handler);
reader.parse(new InputSource(sourceUrl.openStream()));
return handler.getRecords();
}
catch (final Exception e)
{
Log.e("Error", "Error parsing from url", e);
return null;
}
}
public ArrayList<DetailBean> parseFromInputStream(final InputStream in)
{
try
{
final SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
final XmlHandler handler = new XmlHandler();
parser.parse(in, handler);
return handler.getRecords();
}
catch (final Exception e)
{
Log.e("Error", "Error parsing from InputStream", e);
return null;
}
}
public ArrayList<DetailBean> parseFromInputSource(final InputSource is)
{
try
{
final SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
final XmlHandler handler = new XmlHandler();
parser.parse(is, handler);
return handler.getRecords();
}
catch (final Exception e)
{
Log.e("Error", "Error parsing from InputSource", e);
return null;
}
}
}
Your SectionedDemo
activity should extend ExpandableListActivity
instead of ListActivity
, and its onCreate
method should look like:
@Override
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
getExpandableListView().setGroupIndicator(null);
getExpandableListView().setDivider(null);
getExpandableListView().setDividerHeight(0);
parser = new XmlParser();
result = parser.parseFromUrl(XML_URL);
adapter = new MyExpandableListAdapter();
setListAdapter(adapter);
}
Here is a sample output of an application following the instructions above, and using the xml source you provided:
Here is my main activity class for producing this output (without any resource code):
public class SectionedDemo extends ExpandableListActivity
{
private static final String XML_URL = "http://192.168.5.10/ijoomer_development/index.php?" +
"option=com_ijoomer&plg_name=jomsocial&pview=user&ptask=field_list&userid=80&" +
"sessionid=" + /*ConstantData.session_id +*/ "&tmpl=component";
private MyExpandableListAdapter adapter;
private XmlParser parser;
private ArrayList<DetailBean> result;
@Override
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
getExpandableListView().setGroupIndicator(null);
getExpandableListView().setDivider(null);
getExpandableListView().setDividerHeight(0);
parser = new XmlParser();
result = parser.parseFromUrl(XML_URL);
adapter = new MyExpandableListAdapter();
setListAdapter(adapter);
}
private class MyExpandableListAdapter extends BaseExpandableListAdapter
{
private LayoutInflater inflater;
public MyExpandableListAdapter()
{
inflater = LayoutInflater.from(SectionedDemo.this);
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parentView)
{
final DetailBean group = result.get(groupPosition);
convertView =
isExpanded ? inflater.inflate(R.layout.grouprow_expanded, parentView, false) : inflater.inflate(
R.layout.grouprow, parentView, false);
convertView.setBackgroundResource(isExpanded ? R.drawable.back_grouprow_ok_exp : R.drawable.back_grouprow_ok);
((TextView) convertView.findViewById(R.id.groupname)).setText(group.getGroupName());
if (isExpanded)
((TextView) convertView.findViewById(R.id.code)).setText("code " + group.getCode());
return convertView;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parentView)
{
final DetailBean group = result.get(groupPosition);
final Field field = group.getFields().get(childPosition);
convertView = inflater.inflate(R.layout.childrow, parentView, false);
convertView.setBackgroundResource(field.isRequired() ? R.drawable.back_childrow_critical
: R.drawable.back_childrow_unknown);
convertView.findViewById(R.id.fieldname).setBackgroundResource(
field.isRequired() ? R.color.red_fader : R.color.blue_fader);
((TextView) convertView.findViewById(R.id.fieldname)).setText("Name: " + field.getName());
((TextView) convertView.findViewById(R.id.value)).setText("Value: " + field.getValue());
((TextView) convertView.findViewById(R.id.id)).setText("ID: " + field.getId() + "");
((TextView) convertView.findViewById(R.id.type)).setText("Type: " + field.getType());
CheckBox checkbox = (CheckBox) convertView.findViewById(R.id.required);
checkbox.setChecked(field.isRequired());
return convertView;
}
@Override
public Object getChild(int groupPosition, int childPosition)
{
return result.get(groupPosition).getFields().get(childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition)
{
return childPosition;
}
@Override
public int getChildrenCount(int groupPosition)
{
return result.get(groupPosition).getFields().size();
}
@Override
public Object getGroup(int groupPosition)
{
return result.get(groupPosition);
}
@Override
public int getGroupCount()
{
return result.size();
}
@Override
public long getGroupId(int groupPosition)
{
return groupPosition;
}
@Override
public void notifyDataSetChanged()
{
super.notifyDataSetChanged();
}
@Override
public boolean isEmpty()
{
return ((result == null) || result.isEmpty());
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition)
{
return true;
}
@Override
public boolean hasStableIds()
{
return true;
}
@Override
public boolean areAllItemsEnabled()
{
return true;
}
}
}
精彩评论