Android:将数据从 DefaultHandler 类传递到 Activity?

发布于 2024-11-02 13:47:41 字数 1080 浏览 1 评论 0原文

我正在开发一个 Android 应用程序,我将在其中大量使用 SAX。我想知道将“数据”或结果从我的处理程序发送回我的活动的最佳方式是什么?

IMO 在我的处理程序中调用意图或创建 toast 等有点混乱,我更喜欢从我的活动中执行这些操作,具体取决于我的处理程序中发生的情况。

你们觉得怎么样?我应该如何以干净的方式做到这一点?

下面是一个代码示例:

        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;

            }

我不想从我的处理程序类中显示这些 Toast。

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?

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?

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;

            }

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

烟酉 2024-11-09 13:47:41

我在当前的应用程序中完成了大量的 XML 解析,这是这个示例 帮了很多忙。

就设计而言,我相信您应该使用自定义 SAX 处理程序,而不是 toasts 或意图广播等,它将在开始的 XML 元素处实例化 Parse 对象。该对象是 XML 项目的表示。因此,该对象可能是“汽车”,并且具有“门”、“颜色”、“车轮”的 setter/getter。当您在 SAX 解析器中解析数据时,您将设置这些值。当 SAX 解析器完成解析时,您调用解析器,让它将对象传回您的活动,该活动充满了 XML 中的所有汽车。就我而言,我实际上填充了传回的对象的列表/数组。该示例仅处理一组数据。无论如何,该链接解释了一切。

编辑:只是查看我的代码,实际上我在处理程序中所做的是构建 ParsedData 集对象的数组,这些对象在解析完成后通过 getParsedData() 传递回活动。以下是一些重要的代码:

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 元素

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 对象数组,这些对象被传递回活动。这是一个比使用 toast 或广播更好的解决方案,因为它是一个更加解耦的解决方案

编辑 2:我链接的网站第二页上的第一篇文章提到了如何解析像我这样的多个 XML 元素。请参阅此处(解析多个 xml 元素)。

我希望这对你有帮助

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

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.

Edit: 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 handler:

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));
    }
}

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";
    }
}

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

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).

I hope this helps you out

余生再见 2024-11-09 13:47:41

您可以:

  1. 将活动传递到 XML 中
    处理程序,将其存储为实例
    变量,并将其回调为
    需要。

  2. 在您的活动中创建一个静态成员并将活动分配给
    当它被创建时。然后,从您的 XML 处理程序中,您可以引用 ActivityClass.getMyActivity() [例如] 并调用一些方法来弹出 toast。

iOS/Mac OS X 有一个称为“通知”的概念,其中任何代码都可以注册对某些事件的兴趣。如果有 Android 等效项,那就是另一种方法来做到这一点,即 XML 处理程序可以广播某种失败事件/通知,而 Activity 可以侦听它并弹出 toast。

You could:

  1. Pass the activity into the XML
    handler, store it as an instance
    variable, and call back to it as
    needed.

  2. Create a static member in your activity and assign the activity to
    it when it's created. Then from your XML handler, you could reference ActivityClass.getMyActivity() [for example] and call some method to pop toast.

iOS/Mac OS X has a concept called "notifications" where any code can register an interest in some event. If there's an Android equivalent, that'd be another way to do this, i. e. the XML handler could broadcast some sort of failure event/notification and the Activity could listen for it and pop toast.

轻许诺言 2024-11-09 13:47:41

我会使用自定义 Handler 我在我的活动中创建并将其传递给 XML 解析器。然后在这个解析器中我可以调用一些 < code>sendMessage 在自定义处理程序上。

处理程序的示例用法 此处

I would use a custom Handler which I created in my activity and passing it to the XML parser. Then within this parser I could call some sendMessage on the custom Handler.

An example usage of Handler here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文