здесь
Я пытаюсь распечатать динамически определенное имя группы и ее дочерние данные, такие как заголовок и его содержимое.
, но я не могу напечатать имя группы в заголовке, который не является проблемой.
Но проблема в том, что я также получаю имя группы вместо дочернего содержимого
ссылка на мой код
вот код: 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);
}
};
}
и код для 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-файла, который я хочу проанализировать
<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 = SAXParserFactory.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);
}
}