Как обновить пользовательский интерфейс с помощью AsyncTask - PullRequest
0 голосов
/ 19 мая 2011

Я хочу обновить интерфейс с некоторой информацией о данных в течение определенного интервала, поэтому кто-нибудь может помочь мне сделать это с помощью AsyncTask?

Ответы [ 2 ]

1 голос
/ 19 мая 2011

используйте onProgressUpdate() метод AsyncTask. Это выполняется в потоке пользовательского интерфейса.

0 голосов
/ 19 мая 2011

Попробуйте Asynctask, как показано здесь:

try{
    class test extends AsyncTask{


         TextView tv_per;
         int mprogress;

        Dialog UpdateDialog = new Dialog(ClassContext);

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            mprogress = 0;

            UpdateDialog.setTitle(getResources().getString(R.string.app_name));
            UpdateDialog.setContentView(R.layout.horizontalprogressdialog);
            TextView dialog_message =  (TextView)UpdateDialog.findViewById(R.id.titleTvLeft);
            tv_per = (TextView)UpdateDialog.findViewById(R.id.hpd_tv_percentage);
            dialog_message.setText(getResources().getString(R.string.dialog_retrieving_data));
            dialog_message.setGravity(Gravity.RIGHT);
            UpdateDialog.setCancelable(false);
            UpdateDialog.show();
            super.onPreExecute();
        }



        @Override
        protected void onProgressUpdate(Object... values) {
            // TODO Auto-generated method stub
            ProgressBar update = (ProgressBar)UpdateDialog.findViewById(R.id.horizontalProgressBar);
            update.setProgress((Integer) values[0]);
            int percent =  (Integer) values[0];
            if(percent>=100)
            {
                percent=100;
            }
            tv_per = (TextView)UpdateDialog.findViewById(R.id.hpd_tv_percentage);
             tv_per.setText(""+percent);
        }



        @Override
        protected Object doInBackground(Object... params) {
            // TODO Auto-generated method stub
            //your code of UI operation
}

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

     }
     new test().execute(null);

 }
 catch(Exception e)
 {
     e.printStackTrace();
 }

Также обратитесь к этой ссылке: Получать данные с сервера и обновлять пользовательский интерфейс при получении данных?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...