在后台获取数据并在UI中显示 [英] Fetch data in background and display in UI

查看:102
本文介绍了在后台获取数据并在UI中显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用凌空抽空,使用volley来获取数据并在用户界面中显示,因此加载数据需要花费时间.我想发生的是,我想在启动屏幕或加载屏幕期间在后台获取数据,并将其显示在用户interface中.我希望提取部分在另一个activity中完成,它应该是背景service,应该在"main activity"中调用它以填充字段.

I'm using volley to fetch data using volley and display in User Interface, and it takes time to load the data. What i want to happen is, I want to fetch the data in background during the splash screen or loading screen and display it in User interface. I want the fetching part to be done in another activity and it should be a background service and this should be called in "main activity" to populate the fields.

推荐答案

您可以尝试使用

You can try using the AsyncTask. So the process goes like that:

  • In onPreExecute() you can show a ProgresDialog or a ProgresBar to visualize your loading process
  • in doInBackground(Params...) you start loading your data
  • and in onPostExecute(Result) you display your data in UI and hide your ProgresDialog/ProgresBar.

使用AsyncTask的示例.

这样做:

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

    @Override
    protected String doInBackground(String... params) {
        //Start loading your data 

        return "Data loaded";
    }

    @Override
    protected void onPostExecute(String result) {
       //Update your UI with data you loaded/start your activity with loaded data

    }

    @Override
    protected void onPreExecute(){     
    }

    @Override
    protected void onProgressUpdate(Void... values) {
    }
}

这篇关于在后台获取数据并在UI中显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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