简体   繁体   English

收集数据后设置UI线程的值?

[英]Setting values for UI Thread after data is collected?

this is a theoretical question! 这是一个理论问题!

I am currently developing an android application.There I would like to collect data and then edit the UI. 我目前正在开发一个Android应用程序。我想收集数据,然后编辑UI。 But when I am waiting for finishing data collection I am obviously blocking the UI Thread(). 但是当我等待完成数据收集时,我显然阻止了UI Thread()。

So the question is: How can I make app to wait for the data collection without blocking the UI Thread? 所以问题是:如何在不阻止UI线程的情况下让应用程序等待数据收集? Is a loading screen with the appsymbol and a progress bar an option? 带有appsymbol的加载屏幕和进度条是一个选项吗?

Edit: To make it clear: My data collect is seperated in several small data fetches! 编辑:说清楚:我的数据收集分为几个小数据提取!

Edit 2: I am collecting the data form the internet! 编辑2:我正在从互联网上收集数据!

Thanks in advance! 提前致谢!

You can use an AsyncTask for simple data fetch. 您可以使用AsyncTask进行简单的数据提取。 If its more complicated use Volley 如果它更复杂的使用Volley

Declare an AsyncTask, then simply display ProgressDialog on preExecute of AsyncTask. 声明一个AsyncTask,然后只在AsyncTask的preExecute上显示ProgressDialog。 Do the time consuming and application freezing things like retrieving data from Link on doInBackground. 耗费时间和应用程序会冻结诸如从doInBackground上的Link检索数据。 Do the post Link retrieved works in the onPostExecute of asyncTask and dismiss the `ProgressDialog. 检索到的链接是否在asyncTask的onPostExecute中工作并解除了`ProgressDialog。 AsyncTask runs on a diffrent thread from the main activity and hence the application doesn't freeze. AsyncTask在主活动的不同线程上运行,因此应用程序不会冻结。

ASYNC TASK ASYNC任务

class ClassName extends AsyncTask<String, Void, Integer> {
    protected void onPreExecute() {
        progress = ProgressDialog.show(context, "", "Loading ...",
                false, true);
    }

    @Override
    protected Integer doInBackground(String... params) {
        // Your Code
    }

    protected void onPostExecute(Integer result) {

        if (progress != null) {
            progress.dismiss();
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM