安卓:将数据从DefaultHandler类到活动? [英] Android: passing data from DefaultHandler class to Activity?

查看:211
本文介绍了安卓:将数据从DefaultHandler类到活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Andr​​oid应用程序的工作,我将使用SAX不少。我想知道什么会给人以数据或结果从我的处理程序回到我的活动最好的方法是什么?

I'm working on an android application where I'll be using the SAX quite a lot. I was wondering what would be the best way to send 'data' or results from my handler back to my activity?

国际海事组织它是一种凌乱叫意图或我的处理程序中创建烤面包等,我想prefere做这种事情从我的活动,这取决于我的处理程序中发生的事情。

IMO it's kind of messy to call intents or create toasts etc within my handler, and I'd prefere to do those kind of things from my activity, depending on what happens within my handler.

你们有什么感想?我应该怎么做一个干净的方式?

What do you guys think? How should I do this in a clean way?

下面是一个code例如:

Here's a code example:

        public void startElement(String n, String l, String q, Attributes a) throws SAXException{
            if(l == "login") in_loginTag = true;

            if(l == "error") {

                    if(Integer.parseInt(a.getValue("value")) == 1)
                        Toast.makeText(getApplicationContext(), "Couldn't connect to Database", Toast.LENGTH_SHORT).show();
                    if(Integer.parseInt(a.getValue("value")) == 2)
                        Toast.makeText(getApplicationContext(), "Error in Database: Table missing", Toast.LENGTH_SHORT).show();
                    if(Integer.parseInt(a.getValue("value")) == 3)
                        Toast.makeText(getApplicationContext(), "Invalid username and/or password", Toast.LENGTH_SHORT).show();

                error_occured = true;

            }

我prefere不是从我的处理程序类显示这些祝酒词。

I'd prefere not showing these Toasts from my handler class.

推荐答案

我在我目前的应用程序做了相当多的XML解析,这是的这个例子帮了不少忙。

I've done quite a bit of XML parsing in my current app and this is this example helped a lot .

设计明智的,我相信,而不是祝酒词,或者你应该使用一个自定义的SAX处理程序,将实例一开始XML元素解析对象的意图广播等。这个对象就是你的XML项目的重新presentation。所以,也许对象将是汽车,并有二传手/ getter方法​​门,颜色,轮毂。当你的解析数据SAX解析器,你会设置这些值。当SAX解析器解析完成后调用解析器把它传回的对象,你的行为是完全与你的XML的所有汽车。在我来说,我居然填充我的对象的列表/阵列被传回。该示例仅处理一组数据。反正该链接可以解释这一切。

Design wise i believe instead of toasts or intent broadcasting etc you should be using a custom SAX Handler which will instantiate a Parse object at the beginning XML element. This object is a representation of your XML items. So maybe the object will be Car and have a setter/getter for Door, Colour, Wheels. As your parsing the data in the SAX parser you will set those values. when the SAX parser finishes parsing you call your parser to have it pass back the object to your activity which is full with all cars from your XML. In my case i actually populate a list/array of my objects which are passed back. The example only deals with one set of data. Anyway that link explains it all.

编辑:只是看着我的code,其实我做什么,我的处理程序是建立我ParsedData阵列设置被传递回活动解析后的对象是完全通过getParsedData()。下面是一些重要的code:

Was just looking at my code, actually what I do in my handler is build an array of my ParsedData set objects which are passed back to the activity after parsing is complete via getParsedData(). Here is some of the important code:

XML处理程序:

private boolean in_IdSite;
private boolean in_SiteName;

private ArrayList<ParsedChannelDataSet> list = new ArrayList<ParsedChannelDataSet>();

public ArrayList<ParsedChannelDataSet> getParsedData() {
    return this.list;
}

@Override
public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
    // first tag parsed so add a new ParsedEventDataSet object
    if(localName.equals("stmSiteUser")) {
        list.add(new ParsedChannelDataSet());
    } else if (localName.equals("idSite")) {
        this.in_IdSite = true;
    } else if (localName.equals("siteName")) {
        this.in_SiteName = true;    
    }
}

@Override
public void endElement(String namespaceURI, String localName, String qName) throws SAXException {
    if (localName.equals("idSite")) {
        this.in_IdSite = false;
    } else if (localName.equals("siteName")) {
        this.in_SiteName = false;
    }
}

@Override
public void characters(char ch[], int start, int length) {
    // determine if any tag is current, get data from tag and populate into ParsedEventDataSet
    if (this.in_IdSite) {
        this.list.get(this.list.size()-1).setExtractedIdSite(new String(ch, start, length));
    } else if (this.in_SiteName) {
        this.list.get(this.list.size()-1).setExtractedSiteName(new String(ch, start, length));
    }
}

下面是我的示例ParsedDataSampleSet(任何你想这是可以调用)显然,你要替换siteName的和idSite别的东西了。这些只是我的XML元素

Here is my sample ParsedDataSampleSet (this can be called whatever you want) obviously you want to replace siteName and idSite with something else too. These are just my XML elements

public class ParsedChannelDataSet {
    private String extractedIdSite = null;
    private String extractedSiteName = null;


    public String getExtractedIdSite() {
        return extractedIdSite;
    }

    public void setExtractedIdSite(String _extractedIdSite) {
        this.extractedIdSite = _extractedIdSite;
    }   

    public String getExtractedSiteName() {
        return extractedSiteName;
    }

    public void setExtractedSiteName(String _extractedSiteName) {
        Log.d("", _extractedSiteName);
        this.extractedSiteName = _extractedSiteName;
    }       


    public String toString() {
        /* TODO */
        return "todo";
    }
}

所以你可以看到我建立被传递回活动ParsedChannelDataSet对象的数组。这是一个比使用烤面包或广播更好的解决方案,因为它更解耦的解决方案

So you can see I build an array of ParsedChannelDataSet objects which are passed back to the activity. this is a far better solution than using toast or broadcasts because its a more decoupled solution

编辑2:网站的这个第2页上的第一篇文章我联系提到有关分析像我这样的多个XML元素。看到这里(<一href="http://www.anddev.org/novice-tutorials-f8/parsing-xml-from-the-net-using-the-saxparser-t353-15.html"相对=nofollow>解析多个XML元素的)。

Edit 2: The first post on this 2nd page of the site I linked mentions about parsing multiple XML elements like mine. See here (parse multiple xml elements).

我希望这可以帮助你

这篇关于安卓:将数据从DefaultHandler类到活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆