Android:加载数据然后通知活动?还记录了一个失败的方法!

发布于 2024-08-07 02:34:28 字数 561 浏览 2 评论 0原文

我只是尝试了一种愚蠢的方法,它使我的应用程序崩溃了...基本上我有一个包含三个选项卡的活动(包含三个活动)。每个选项卡都从从网上下载的 xml 文件获取输入。一切都很好,但是当我启动我的应用程序时,它已经下载了 xml 文件,并且有一个“等待”时间。

我设法通过添加启动画面来解决这个问题。它看起来很漂亮,但问题是当我单击第二个选项卡时,它仍然必须从网上获取列表,所以它现在看起来很难看......它在显示列表之前等待。所以我所做的就是设计一个只下载 xml 文件的 AsyncTask。在我的主要活动中,我最初生成两个任务并发送 URL 和 Intent 作为参数。在选项卡内启动的活动中,我使用 wait()。在 AsyncTask 中,下载完成后,我使用 notification() 通知 Intent。这下崩溃了!当然,我没想到它会起作用,只是想尝试一下:)写它是为了我可以获得关于失败原因的反馈,或者防止其他人在这方面浪费时间......

现在,我是肯定很多人都面临着选项卡内“等待”时间的问题。我该如何解决这个问题?我正在考虑要么调暗屏幕,然后显示一系列 toast,要么在选项卡内显示进度指示器,要么预取 xml 文件...我不知道如何实现这些...任何想法?

I just tried a stupid approach and it crashed my app... Basically I have an activity that has three tabs (containing three activities). Each of the tabs gets its input from an xml file downloaded off the net. Everything is fine but when I start my app, it has download the xml file and there's a "wait" time for this.

I managed to get around this by adding a splash screen. It looks beautiful but the problem is when I click on the second tab, it still has to get the list off the net, so it looks ugly now... It waits before displaying the list. So what I did was design an AsyncTask that just downloads the xml file. In my main activity, I spawn two tasks initially and send the URL and Intent as parameters. And inside the acitivities that start inside the tabs, I use a wait(). Inside the AsyncTask, after it is done with the download, I notify the Intent using notify(). This crashed! Of course, I didn't expect it to work but just wanted to try :) Writing it so that I can either get a feedback as to why this failed or to prevent others from wasting their time on this...

Now, I am sure many face the problem of a "wait" time inside the tabs. How do I solve this? I am thinking of either dimming the screen and then displaying a series of toasts or display a progress indicator inside the tabs or pre-fetching the xml files... I don't have a clue on how these can be achieved... Any thoughts?

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

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

发布评论

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

评论(2

淡看悲欢离合 2024-08-14 02:34:28

信用:马克。谢谢!

问题:当您的应用程序忙于执行某些工作时显示进度指示器

方法:

public class Approach extends ListActivity {

    ProgressDialog myProgressDialog = null; 
    ListView myList = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        myList = getListView();

        myProgressDialog = ProgressDialog.show(getParent(),       
                "Please wait...", "Doing extreme calculations...", true); 
        //Do some calculations
            myProgressDialog.dismiss();

    }
}

存在一些挑战(例如更新一些 UI 元素)。如果需要,您可能希望生成一个不同的线程来进行计算。

另外,如果您对此感兴趣,您可能也对 Matt 的方法感兴趣:android-显示不确定-进度条-in-tabhost-activity

Credit: To Mark. Thanks!

Problem: Display a Progress Indicator when your application is busy doing some work

Approach:

public class Approach extends ListActivity {

    ProgressDialog myProgressDialog = null; 
    ListView myList = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        myList = getListView();

        myProgressDialog = ProgressDialog.show(getParent(),       
                "Please wait...", "Doing extreme calculations...", true); 
        //Do some calculations
            myProgressDialog.dismiss();

    }
}

There are a few challenges (like updating some UI elements). You might want to spawn a different thread to do your calculations if needed.

Also, if you are interested in this, you might be interested in Matt's approach too: android-showing-indeterminate-progress-bar-in-tabhost-activity

恰似旧人归 2024-08-14 02:34:28

进度对话框

或者,让选项卡具有 android:visibility="gone" 直到数据准备就绪,然后使它们可见。在此期间,显示某种加载图形(可能应用了 RotateAnimation)。

ProgressDialog.

Or, make the tabs have android:visibility="gone" until such time as the data is ready, then make them visible. In the interim, show some sort of loading graphic (perhaps with a RotateAnimation applied).

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