简化的Android SAX XML解析问题

时间:2010-12-03 04:14:35

标签: android xml sax

我已经看到了简化Android SAX XML Parsing的方法。

http://www.ibm.com/developerworks/opensource/library/x-android/

我已经解析了数据,但我似乎无法使用循环TextViews将其显示在屏幕上。

这是我的AndroidSaxFeedParser类...

public class AndroidSaxFeedParser extends BaseFeedParser{

final MagazineList magazineList = new MagazineList();

    protected AndroidSaxFeedParser(String magazinefeaturedURL) {
        super(magazinefeaturedURL);
        // TODO Auto-generated constructor stub
    }

    @Override
    public List<MagazineList> parse() {
        // TODO Auto-generated method stub


        RootElement root = new RootElement("maintag");

        final List<MagazineList> magazines = new ArrayList<MagazineList>();
        Element item = root.getChild("item");

        item.setEndElementListener(new EndElementListener(){

            @Override
            public void end() {
                // TODO Auto-generated method stub
                magazines.add(magazineList);
            }

        });

        item.getChild("name").setEndTextElementListener(new EndTextElementListener(){
            @Override
            public void end(String name) {
                // TODO Auto-generated method stub
                magazineList.setName(name);
            }
        });


        item.getChild("imgurl").setEndTextElementListener(new EndTextElementListener() {

            @Override
            public void end(String imgurl) {
                // TODO Auto-generated method stub
                magazineList.setImgurl(imgurl);
            }
        });

        item.getChild("date").setEndTextElementListener(new EndTextElementListener() {

            @Override
            public void end(String date) {
                // TODO Auto-generated method stub
                magazineList.setDate(date);
            }
        });

        item.getChild("description").setEndTextElementListener(new EndTextElementListener() {

            @Override
            public void end(String description) {
                // TODO Auto-generated method stub
                magazineList.setDescription(description);
            }
        });

        item.getChild("price").setEndTextElementListener(new EndTextElementListener() {

            @Override
            public void end(String price) {
                // TODO Auto-generated method stub
                magazineList.setPrice(price);
            }
        });

        item.getChild("download").setEndTextElementListener(new EndTextElementListener() {

            @Override
            public void end(String download) {
                // TODO Auto-generated method stub
                magazineList.setDownload(download);
            }
        });


        return magazines;
    }

}

这是我的对象模型类......

public class MagazineList {

private ArrayList<String> name = new ArrayList<String>();
private ArrayList<String> imgurl = new ArrayList<String>();
private ArrayList<String> date = new ArrayList<String>();
private ArrayList<String> price = new ArrayList<String>();
private ArrayList<String> description = new ArrayList<String>();
private ArrayList<String> download = new ArrayList<String>(); 

public ArrayList<String> getName() {
    return name;
}

public void setName(String name) {
    this.name.add(name);
}

public ArrayList<String> getImgurl(){
    return imgurl;
}

public void setImgurl(String imgurl){
    this.imgurl.add(imgurl);
}

public ArrayList<String> getDate() {
    return date;
}

public void setDate(String date) {
    this.date.add(date);
}

public ArrayList<String> getPrice() {
    return price;
}

public void setPrice(String price) {
    this.price.add(price);
}

public ArrayList<String> getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description.add(description);
}

public ArrayList<String> getDownload() {
    return download;
}

public void setDownload(String download) {
    this.download.add(download);
}
}

现在的问题是......如何让它显示在主Activity上?

public void onCreate(Bundle savedInstanceState) {
    MagazineList magazineList = null;
private List<MagazineList> magazines = null;



    LinearLayout layout = new LinearLayout(this);
    layout.setOrientation(1);


    TextView name[];
    TextView imgurl[];
    TextView date[];
    TextView description[];
    TextView price[];
    TextView download[];

    AndroidSaxFeedParser saxparser = new  AndroidSaxFeedParser(<my website>);
    magazines = saxparser.parse();
    for(MagazineList magazineList : magazines){
         name = new TextView[magazineList.getName().size()];
         imgurl = new TextView[magazineList.getName().size()];
         date = new TextView[magazineList.getName().size()];
         description = new TextView[magazineList.getName().size()];
         price = new TextView[magazineList.getName().size()];
         download = new TextView[magazineList.getName().size()];


    (Supposingly I should get my data from here... What's next? Perhaps I have some syntax errors when declaring TextViews, I'm not sure. Please help out.)
    }

}

1 个答案:

答案 0 :(得分:0)

嗯,我不是Java专家,但我从来没有见过像你正在做的那些TextViews。这是数组符号。

改为尝试(假设解析代码正常工作)

等等,我完全被你在使用MagazineList课程所做的事情所抛弃。好的,首先,对于TextViews,你会想要使用这样的东西:

name = new TextView(this);
name.setText(/*your magazinelist getter for name*/);
name.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
layout.addView(name);

这些是否列入清单?你最终在做什么?如果您只是想确保解析内容,只需运行调试器并在运行解析后检查magazinelist对象。

至于你的MagazineList课......也许我误解了,但你真的应该有一本杂志课,定义一本杂志及其名称,日期等,然后你就可以使用{{1创建杂志列表。然后,您可以使用foreach循环来迭代它们,或者将它们调整为自定义ListView。

示例杂志类:

ArrayList<Magazine>

然后你只需要扩展一个ArrayAdapter,它将接收你的ArrayList或Magazine []并将它显示到ListView中。