问题
I am getting wrong listview data. I am using SAX parsing to get data from Web-service. Service is good, but I don't know where the mistake is? Here my code:
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.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class SaxParserDemoActivity extends ListActivity {
/** Called when the activity is first created. */
ArrayList<String> al_sno=new ArrayList<String>();
ArrayList<String> al_sname=new ArrayList<String>();
ArrayList<String> al_sclass=new ArrayList<String>();
ArrayList<String> al_sphno=new ArrayList<String>();
ArrayList<String> al_semail=new ArrayList<String>();
SAXParserFactory spf;
SAXParser sp;
XMLReader xr;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try{
spf=SAXParserFactory.newInstance();
sp=spf.newSAXParser();
xr=sp.getXMLReader();
URL sourceUrl = new URL(
"http://xxxxxxxxxxxxxxxxxxx");
MyHandler mh=new MyHandler();
xr.setContentHandler(mh);
xr.parse(new InputSource(sourceUrl.openStream()));
//.getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.main);
setListAdapter(new MyAdapter());
}
catch(Exception e){}
setListAdapter(new MyAdapter());
}
class MyAdapter extends BaseAdapter{
@Override
public int getCount() {
// TODO Auto-generated method stub
return al_sclass.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
LayoutInflater li=(LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
View v=li.inflate(R.layout.second, null);
TextView tv1=(TextView)v.findViewById(R.id.text1);
tv1.setText(al_sno.get(arg0));
TextView tv2=(TextView)v.findViewById(R.id.text2);
tv2.setText(al_sname.get(arg0));
return v;
}
}
class MyHandler extends DefaultHandler{
boolean is_sno=false;
boolean is_sname=false;
@Override
public void startDocument() throws SAXException {
// TODO Auto-generated method stub
super.startDocument();
}
@Override
public void startElement(String uri, String localName, String name,
Attributes attributes) throws SAXException {
super.startElement(uri, localName, name, attributes);
if(localName.equals("ID")){
is_sno=true;
}
else if(localName.equals("Name")){
is_sname=true;
}
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
// TODO Auto-generated method stub
super.characters(ch, start, length);
if(is_sno){
al_sno.add(new String(ch,start,length));
}
else if(is_sname){
al_sname.add(new String(ch,start,length));
}
}
@Override
public void endElement(String uri, String localName, String name)
throws SAXException {
// TODO Auto-generated method stub
super.endElement(uri, localName, name);
if(localName.equals("ID")){
is_sno=false;
}
else if(localName.equals("Name")){
is_sname=false;
}
}
@Override
public void endDocument() throws SAXException {
// TODO Auto-generated method stub
super.endDocument();
}
}
}
My output screen:
.You can see here "Alabama A&M" is splitting into 3 rows.
My logcat is
11-27 11:56:15.516: D/PhoneWindow(429): couldn't save which view has focus because the focused view com.android.internal.policy.impl.PhoneWindow$DecorView@40549d60 has no id.
11-27 11:56:15.976: D/dalvikvm(429): GC_CONCURRENT freed 218K, 50% free 2958K/5831K, external 1064K/1413K, paused 17ms+4ms
11-27 11:56:17.876: V/tea(429): Air Force
11-27 11:56:17.929: V/tea(429): Akron
11-27 11:56:17.946: V/tea(429): Alabama
11-27 11:56:17.986: V/tea(429): Alabama A
11-27 11:56:17.996: V/tea(429): &
11-27 11:56:18.036: V/tea(429): M
11-27 11:56:18.046: V/tea(429): Alabama State
11-27 11:56:18.066: V/tea(429): Albany
11-27 11:56:18.156: V/tea(429): Air Force
11-27 11:56:18.166: V/tea(429): Akron
11-27 11:56:18.196: V/tea(429): Alabama
11-27 11:56:18.229: V/tea(429): Alabama A
11-27 11:56:18.246: V/tea(429): &
11-27 11:56:18.274: V/tea(429): M
11-27 11:56:18.286: V/tea(429): Alabama State
11-27 11:56:18.326: V/tea(429): Albany
11-27 11:56:18.376: V/tea(429): Air Force
11-27 11:56:18.396: V/tea(429): Akron
11-27 11:56:18.426: V/tea(429): Alabama
11-27 11:56:18.456: V/tea(429): Alabama A
11-27 11:56:18.467: V/tea(429): &
11-27 11:56:18.504: V/tea(429): M
11-27 11:56:18.526: V/tea(429): Alabama State
11-27 11:56:18.636: V/tea(429): Albany
11-27 11:56:18.756: D/dalvikvm(429): GC_CONCURRENT freed 173K, 48% free 3188K/6023K, external 1056K/1413K, paused 19ms+6ms
11-27 11:56:19.097: I/ActivityManager(60): Displayed com.ei.s/.Teamlist: +3s566ms
My XML Data is...
<Table diffgr:id="Table2" msdata:rowOrder="1">
<ID>47</ID>
<Name>Akron</Name>
</Table>
<Table diffgr:id="Table3" msdata:rowOrder="2">
<ID>73</ID>
<Name>Alabama</Name>
</Table>
<Table diffgr:id="Table4" msdata:rowOrder="3">
<ID>356</ID>
<Name>Alabama A&M</Name>
</Table>
<Table diffgr:id="Table5" msdata:rowOrder="4">
<ID>187</ID>
<Name>Alabama State</Name>
</Table>
I am getting correct data in WEB-Service but not in Android app. Please help me that where I am making mistake here?
回答1:
The code below(modifying your MyHandler
class) should keep your node text together:
// a field in the MyHandler class:
boolean mIsSegment = false;
// ...
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
// TODO Auto-generated method stub
super.characters(ch, start, length);
if (is_sno) {
al_sno.add(new String(ch, start, length));
} else if (is_sname) {
if (!mIsSegment) {
al_sname.add(new String(ch, start, length));
} else {
al_sname.set(al_sname.size() - 1,
al_sname.get(al_sname.size() - 1)
+ new String(ch, start, length));
}
mIsSegment = true;
}
}
@Override
public void endElement(String uri, String localName, String name)
throws SAXException {
// TODO Auto-generated method stub
super.endElement(uri, localName, name);
if (localName.equals("ID")) {
is_sno = false;
} else if (localName.equals("Name")) {
is_sname = false;
mIsSegment = false;
}
}
// ...
The SAXParser
is breaking the name text in multiple pieces and you're adding each of this pieces as a single item in the list. Check this answer. Also there is no reason to call setListdapter
twice(just call it once at the end of the onCreate
method).
来源:https://stackoverflow.com/questions/13578630/listview-data-spliting-into-another-row-after-sax-parsing