English 中文(简体)
进展 诊断不正确
原标题:ProgressDialog does not show up right away

我试图从互联网上收集数据,然后向用户展示数据。 这一行动通常需要3秒钟,我要说,我正在装上数据。 问题是,“进步”只是在装载几乎完成时才显示出来。 这是我的法典。

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Button load_routes = (Button)findViewById(R.id.load_routes);

    load_routes.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View v)
        {
            task_complete = false;
            progressBar = new ProgressDialog(v.getContext());
            progressBar.setCancelable(false);
            progressBar.setMessage("Fetching routes data ...");
            progressBar.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            progressBar.show();

            if(haveNetworkConnection())
            {
                loading_routes_data(v);
            }
            else
            {
                progressBar.dismiss();
            }
        }
    });
}

这也是我的职责。

private void loading_routes_data(View v)
{
    try
    {
        new Thread(new Runnable()
        {
            public void run()
            {
                while(!task_complete)
                {
                    refresh_routes();
                    try
                    {
                        Thread.sleep(1000);
                    }
                    catch(InterruptedException e)
                    {
                        e.printStackTrace();
                    }
                }
                runOnUiThread(new Runnable()
                {
                    public void run()
                    {
                        progressBar.dismiss();
                    }
                });
            }
        }).start();
    }
    catch(Exception e)
    {
    }

    while(!task_complete)
    {
    }
}

www.un.org/Depts/DGACM/index_chinese.htm 我甚至不清楚我是否应该把它留在 lo中。

因此,这是我的严重问题。 我愿在纽芬兰的点击上显示ProgressDialog,然后请打电话refresh_routes(,一旦refresh_routes(>完整,我就想摆脱进步的辩词。 但我对如何做到这一点感到不快。

如果有人能向我表明,如何以某些条款作为榜样或像我真的感激的话来解决这个问题。 事先感谢你:

最佳回答

您在方法结束时通过打电话while(!task_complete)AsyncTask开展类似行动。 页: 1 ::

private class MyAsyncTask extends AsyncTask<Void, Void, Void> {

    private ProgressDialog progressDialog;
    private Context context;

    public MyAsyncTask(Context context) {

        this.context = context;
    }

    @Override
    protected void onPreExecute() {

        super.onPreExecute();
        progressDialog = ProgressDialog.show( context, null, "Fetching routes data ..." );
    }

    @Override
    protected Void doInBackground( Void... params ) {

        while (!task_complete) {
            refresh_routes();
            try {
                Thread.sleep( 1000 );
            }
            catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        return null;
    }

    @Override
    protected void onPostExecute( Void result ) {

        super.onPostExecute( result );
        progressDialog.dismiss();
    }
}

To execute this, you would do this:

new MyAsyncTask( this ).execute();

页: 1 方法可行,但您可考虑将其功能移至do InBackground()。

问题回答

rel=“nofollow” 它给你留下了在一项活动中进行多读的巨大特征。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...