如何做到动态加载在android系统? [英] how to do dynamic loading in android?

查看:209
本文介绍了如何做到动态加载在android系统?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有通过XML自带一个RSS feed。但是也有一些有关于他们的信息返回几个事件。该事件与标签返回信息...如:....信息...

i have an rss feed that comes via an XML. There are several events that are returned with information about them. The events are returned with tags...for eg: ....info...

只要我遇到的标签,我想更新我使用,以显示事件列表视图。
因此,用户不会看到加载进度对​​话框,而他看到的事件被添加到该列表。
我如何做到这一点。

as soon as i encounter tag, i want to update the listview that i am using to show the events. So the user does not see the loading progress dialog, rather he sees the events getting added to a list. How do i do this.

感谢您提前。

推荐答案

下面就是这样做的用SAX解析器的一种方式伪codeISH例子;

Here's pseudo codeish example for one way of doing this using SAX parser;

// MyParserThread is assumed to be inner class of Activity here.
private class MyParserThread extends Thread implements MyParserObserver {
    private MyParser mParser;
    public MyParserThread() {
       mParser = new MyParser();
       mParser.setObserver(this);
    }
    public void run() {
        try {
            // load xml
            mParser.parse(xml);
        } catch (Exception ex) {
        }
    }
    public void onMyParserEvent(final DataReceivedFromParsing data) {
        runOnUiThread(new Runnable() {
            public void run() {
                 // update data to your UI.
            }
        });
    }
    public void cancel() {
        mParser.cancel();
    }
}

而在你的解析器,你正在实施<一个href=\"http://developer.android.com/reference/org/xml/sax/ContentHandler.html#startElement%28java.lang.String,%20java.lang.String,%20java.lang.String,%20org.xml.sax.Attributes%29\"相对=nofollow> ContentHandler的

public void cancel() {
    mCancelled = true;
}
public void startElement(....) {
    if (mCancelled) {
        // If you want to stop Thread from running, all you have to do
        // is make parsing stop.
        throw new SAXException("Cancelled");
    }
    ....
}

和触发解析一旦你的onCreate被称为是;

And triggering parsing once your onCreate is called would be;

public void onCreate(...) {
    ...
    mParserThread = new MyParserThread();
    mParserThread.start();
    ...
}

现在这个并不完美,但希望给出了一些想法如何为此做线程处理。从根本上讲你只要启动它,并加入取消的功能是较为奖金的 - 例如,对于案件中,你的线程运行时的活动被破坏。

Now this isn't perfect but hopefully gives some idea how to do Thread handling for this purpose. Fundamentally you just have start it, and adding 'cancel' functionality is somewhat more of a bonus - e.g. for cases in which Activity is destroyed while your Thread is running.

这篇关于如何做到动态加载在android系统?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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