活动在使用 TabActivity 显示之前从服务器加载 xml

发布于 2024-12-17 07:03:02 字数 1551 浏览 0 评论 0原文

我使用的 TabActivity 具有 4 个单独的 Activities - 每个选项卡一个。

其中一个Activities 是一个具有自定义ArrayAdapterListView

问题是,当我按 Tab 更改到此视图时,Activity 在视图更改之前加载内容,这看起来好像几秒钟内没有任何反应,直到 xml 被加载和解析等。

我已经寻找了一个示例,但这是我的第一个 Android 应用程序,我很难理解流程。

任何人都可以向我指出一些代码,这些代码将允许我在后台线程中加载内容时立即更改视图(我可以通知用户内容正在加载)

谢谢

编辑 - 我正在从现有的 iOS 应用程序移植代码 - 我不是无法更好地阐明问题,因为我没有意识到在这种情况下 UI 线程如何被阻塞,并且由于现有代码和截止日期的复杂性,我不想对结构进行太多更改。

在看到你的代码 Jennifer 之前,我缩小了问题范围,但这是我使用的解决方案,所以我会将你的代码标记为正确。

这是我使用的,如果它对其他人有帮助,我必须将我调用的函数放在后台线程上触发数据加载,然后在该线程完成其工作时显示内容

这个类是在我的内部声明的,

public class TableView extends ListActivity

这对于我之前没有这样做过;)

public class GetContentTask extends AsyncTask<Void, Void, Void> {


    private ProgressDialog pdialog;

    @Override
    protected void onPreExecute(){ 
       super.onPreExecute();
       pdialog = new ProgressDialog(TableView.this);
       pdialog.setTitle(progressDialogTitle);
       pdialog.setMessage(progressDialogMessage);
       pdialog.show();    
    }

    @Override
    protected void onPostExecute(Void result){
       super.onPostExecute(result);
       setUpAndLoadList(); // the function to display the list and fill it with content
       pdialog.dismiss();
    }


    @Override
    protected Void doInBackground(Void... params) {
        doInitialLoad(); // The function to load any xml data from server
        return null;
    }
 }

I am using a TabActivity with 4 separate Activities - one for each tab.

One of the Activities is a ListView that has a custom ArrayAdapter.

The issue is that when I press the Tab to change to this view, the Activity loads the content in before the view changes, this appears as though nothing happens for a couple of seconds until the xml is loaded and parsed etc.

I have looked for an example but this is my first Android appllication and I am having difficulty in understanding the flow.

Can anyone point me to some code that will allow me to instantly change the view (I can inform user content is loading) while loading the content in the background thread

thank you

EDIT - I am porting code over from an existing iOS app - I wasn't able to better articulate the problem as I didn't realise how the UI thread could be blocked in this situation, and due to the complexity of the existing code and deadline I didn't want to change the structure too much.

I narrowed down the issue before I saw your code Jennifer but it is the solution I used so Ill mark yours as right.

here is what I used if it helps anyone else, I had to put the function I called to trigger the data load onto a background thread and then display the content when that thread had done its work

This class was declared within my

public class TableView extends ListActivity

Which was hard for me to get my head around having not done this before ;)

public class GetContentTask extends AsyncTask<Void, Void, Void> {


    private ProgressDialog pdialog;

    @Override
    protected void onPreExecute(){ 
       super.onPreExecute();
       pdialog = new ProgressDialog(TableView.this);
       pdialog.setTitle(progressDialogTitle);
       pdialog.setMessage(progressDialogMessage);
       pdialog.show();    
    }

    @Override
    protected void onPostExecute(Void result){
       super.onPostExecute(result);
       setUpAndLoadList(); // the function to display the list and fill it with content
       pdialog.dismiss();
    }


    @Override
    protected Void doInBackground(Void... params) {
        doInitialLoad(); // The function to load any xml data from server
        return null;
    }
 }

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

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

发布评论

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

评论(2

凌乱心跳 2024-12-24 07:03:02

您可以使用进度对话框(可以通知用户内容正在加载)

ProgressDialog dialog;

private class XMLOperation extends AsyncTask<String, Void, String> {

/*
     * (non-Javadoc)
     * 
     * @see android.os.AsyncTask#onPreExecute()
     */
    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();

        System.out.println("onPreExecute");

        dialog= ProgressDialog.show(mContext, "", "Loading Content....");
              dialog.setCancelable(false);


    }




        @Override
    protected String doInBackground(String... urls) {

            //do your Background task

    }




protected void onPostExecute(String result) {   //dismiss dialog
        try {
if(dialog.isShowing()){
        dialog.dismiss();
        }



        } catch (Exception exception) {
        dialog.dismiss();
        }
        }

You can use a progress Dialog (can inform user content is loading)

ProgressDialog dialog;

private class XMLOperation extends AsyncTask<String, Void, String> {

/*
     * (non-Javadoc)
     * 
     * @see android.os.AsyncTask#onPreExecute()
     */
    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();

        System.out.println("onPreExecute");

        dialog= ProgressDialog.show(mContext, "", "Loading Content....");
              dialog.setCancelable(false);


    }




        @Override
    protected String doInBackground(String... urls) {

            //do your Background task

    }




protected void onPostExecute(String result) {   //dismiss dialog
        try {
if(dialog.isShowing()){
        dialog.dismiss();
        }



        } catch (Exception exception) {
        dialog.dismiss();
        }
        }
木落 2024-12-24 07:03:02

使用 AsyncTask,或(可能)单独的线程。

http://developer.android.com/reference/android/os/AsyncTask.html

我也会投入 2 美分并说不要使用 TabActivity。只需拥有自己的看起来像选项卡的按钮即可,但这对于本主题来说并不重要。

Use AsyncTask, or (possibly) a separate thread.

http://developer.android.com/reference/android/os/AsyncTask.html

I would also throw in my 2 cents and say don't use TabActivity. Just have your own buttons that look like tabs, but that's not really critical to this topic.

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