【问题标题】:How to resolve an Android listview that is not working properly如何解决无法正常工作的 Android 列表视图
【发布时间】:2011-02-14 14:02:09
【问题描述】:

我正在尝试从 Web 读取 XML 并将它们显示在 Listview 中。在 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);

}

endElement 方法:

    @Override

    public void endElement(String namespaceURI, String localname, String qName)throws SAXException {
        if(localname.equalsIgnoreCase("Business")){
            this.in_business=false;
        }else if(localname.equalsIgnoreCase("local")){
            this.in_local=false;
        }else if(localname.equalsIgnoreCase("ItemName")){
            this.in_itemName=false;
        }else if(localname.equalsIgnoreCase("ItemAddress")){
            this.in_itemAddress=false;
        }else if(localname.equalsIgnoreCase("Latitude")){
            this.in_itemLatitude=false;
        }else if(localname.equalsIgnoreCase("Longitude")){
            this.in_itemLongitude=false;
        }   
}
    @Override
    public void characters(char ch[], int start, int length) {
                if(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;
    }

}

【问题讨论】:

    标签: android xml listview sax


    【解决方案1】:

    我仔细阅读了你的代码,发现你犯了非常严重的错误。

    通过此代码,您只能看到列表中的最后一条记录。

    这样做我提到打击。

    1-:创建一个带变量的类 公共字符串名称,地址....

    2-:在 SearchItemHandler 类中创建一个列表。 公共静态列表=新列表(); 然后 在 xml 的解析中,您应该创建一个 ClassName 的对象(在上面创建) 并通过xml getValue设置类的每个数据成员的值 然后将此对象放入列表中。 在解析结束时,您会在列表中找到三个对象。

    3-: 在 SearchItemListActivity 类中访问这个列表 并获取每个对象的值并制作一个数组适配器。

    4-:将此适配器放入列表中,然后您会在列表中找到三个项目。

    希望对您有帮助,如果您发现任何问题,请正确评论。

    【讨论】:

    • 嗨 RaviShankar。谢谢你的来信。我正在尝试遵循您的建议。我已经编辑了我的问题并添加了一些额外的代码。我真的不明白如何做你建议的第 3 点和第 4 点。所以你能再看一遍我的代码并帮助我更多吗?提前致谢。 :)
    猜你喜欢
    • 1970-01-01
    • 2019-10-24
    • 1970-01-01
    • 2017-04-24
    • 1970-01-01
    • 2022-11-17
    • 1970-01-01
    • 2015-01-09
    • 2012-06-01
    相关资源
    最近更新 更多