在后台获取数据并在UI中显示 [英] Fetch data in background and display in UI
本文介绍了在后台获取数据并在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:
- 在 onPreExecute()中,您可以显示ProgresDialog或ProgresBar来可视化您的加载过程
- 在 doInBackground(Params ...)您开始加载数据
- ,并在 onPostExecute(Result)中显示您在UI中的数据并隐藏您的ProgresDialog/ProgresBar.
- 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.
这样做:
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屋!
查看全文