Привет
Я пытаюсь прочитать XML из Интернета и отобразить их в виде списка. В XML у меня есть 3 записи, и мой ListView показывает только последнюю запись. Было бы здорово, если бы кто-нибудь помог мне выявить ошибки !!
Я использую SAX для чтения из Интернета.
Обработчик:
public class SearchItemHandler extends DefaultHandler{
private boolean in_itemName=false;
private boolean in_itemAddress=false;
private boolean in_itemLatitude=false;
private boolean in_itemLongitude=false;
private boolean in_business=false;
private boolean in_local=false;
private SearchItem sItem=new SearchItem();
public SearchItem getParsedData(){
return this.sItem;
}
public static List<SearchItem> list=new ArrayList<SearchItem>();
@Override
public void startDocument() throws SAXException {
this.sItem = new SearchItem();
}
@Override
public void endDocument() throws SAXException {
// Nothing to do
}
@Override
public void startElement(String namespaceURI,String localname,String qName,Attributes atts)throws SAXException{
if(localname.equalsIgnoreCase("Business")){
this.in_business=true;
}else if(localname.equalsIgnoreCase("local")){
this.in_local=true;
}else if(localname.equalsIgnoreCase("ItemName")){
String itemname=atts.getValue("name");
sItem.setItemName(itemname);
this.in_itemName=true;
}else if(localname.equalsIgnoreCase("ItemAddress")){
String itemaddress=atts.getValue("address");
sItem.setItemAddress(itemaddress);
this.in_itemAddress=true;
}else if(localname.equalsIgnoreCase("Latitude")){
double lat=Double.parseDouble(atts.getValue("lat"));
sItem.setLatitude(lat);
this.in_itemLatitude=true;
}else if(localname.equalsIgnoreCase("Longitude")){
double lon=Double.parseDouble(atts.getValue("lon"));
sItem.setLatitude(lon);
this.in_itemLongitude=true;
}
list.add(sItem);
}
@Override
public void endElement (String namespaceURI, String localname, String qName) выдает SAXException {
если (localname.equalsIgnoreCase ( "Бизнес")) {
this.in_business = ложь;
} else if (localname.equalsIgnoreCase ("local")) {
this.in_local = ложь;
} else if (localname.equalsIgnoreCase ("ItemName")) {
this.in_itemName = ложь;
} else if (localname.equalsIgnoreCase ("ItemAddress")) {
this.in_itemAddress = ложь;
} else if (localname.equalsIgnoreCase ("Latitude")) {
this.in_itemLatitude = ложь;
} else if (localname.equalsIgnoreCase ("Longitude")) {
this.in_itemLongitude = ложь;
}
}
@Override
публичные пустые символы (char ch [], int start, int length) {
если (this.in_local) {
//currentCondition.(new String (ch, start, length));
// sItem.equals (new String (ch, start, length));
}
}
Активность:
public class SearchItemListActivity extends ListActivity{
String param="Bar";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_list);
try {
URL url = new URL("http://www.webitour.dk/service/localbusiness.aspx?cat="+param);
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
/* Get the XMLReader of the SAXParser we created. */
XMLReader xr = sp.getXMLReader();
/* Create a new ContentHandler and apply it to the XML-Reader*/
SearchItemHandler searchHandler=new SearchItemHandler();
xr.setContentHandler(searchHandler);
xr.parse(new InputSource(url.openStream()));
SearchItem sItem=searchHandler.getParsedData();
ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
HashMap<String,String> item = new HashMap<String,String>();
item.put("Name",sItem.getItemName());
item.put("Address", sItem.getItemAddress());
list.add(item);
SimpleAdapter adapter = new SimpleAdapter(
this,
list,
R.layout.custom_text_view,
new String[] {"Name","Address"},
new int[] {R.id.text1,R.id.text2}
);
setListAdapter(adapter);
}catch(Exception e){
}
}
}
Класс SearchItem:
public class SearchItem {
private String itemName;
private String itemAddress;
public SearchItem(){}
public String getItemName(){
return this.itemName;
}
public void setItemName(String itemName){
this.itemName=itemName;
}
public String getItemAddress(){
return this.itemAddress;
}
public void setItemAddress(String itemAddress){
this.itemAddress=itemAddress;
}
}