使用AsyncTask在主Activity的单独类中获取控件

时间:2014-12-01 19:48:51

标签: java android xml android-asynctask

我需要获取控件来更改布局中的值。但是通过类的context参数,它找不到在xml中声明的控件,每次转换都会返回null。

我尝试恢复此数据的类扩展了AsyncTask:

public class WSCatalogo91 extends AsyncTask<Void, ProgressAux, Boolean> 

private ProgressBar mProgressBar;
private TextView mProgressText;

public WSCatalogo91(WSCatalogo91Listener listener) throws SQLException {
        this.listener = listener;
        dh = new DatabaseHelper((Activity) listener);
        confDao = new ConfiguracaoDao(dh.getConnectionSource());
        pDao = new ProdutoDao(dh.getConnectionSource());

        mProgressBar = (ProgressBar) ((Activity) listener).findViewById(R.id.progressBar);
        mProgressText = (TextView) ((Activity) listener).findViewById(R.id.progressBarTextView);
        auxProgresso = new ProgressAux();
    }

mProgressBar和mProgressText为null,但我的上下文不为null,因此它正在排队使用。遵循声明id的xml代码。

<TextView android:text="@string/progressBar" android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:id="@+id/progressBarTextView"
        android:maxWidth="400dp"
        android:maxHeight="400dp"/>

    <ProgressBar
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/progressBar"
        android:indeterminate="false"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="165dp"
        android:minWidth="300dp" />

这表明有一个progressBar和progressBarTextView: This shows that there is a progressBar nor progressBarTextView

WSCatalogo91类:

  public class WSCatalogo91 extends AsyncTask<Void, ProgressAux, Boolean> {

      public WSCatalogo91(WSCatalogo91Listener listener) throws SQLException {
            this.listener = listener;
        dh = new DatabaseHelper((Activity) listener);
        confDao = new ConfiguracaoDao(dh.getConnectionSource());
        pDao = new ProdutoDao(dh.getConnectionSource());

        mProgressBar = (ProgressBar) ((Activity)listener).findViewById(R.id.progressBar);
        mProgressText = (TextView) ((Activity) listener).findViewById(R.id.progressBarTextView);
        auxProgresso = new ProgressAux();
        }

        private Bitmap baixarImagens(String urlComplement, String tDownload, int totalImages) {
            ...
        }

        @Override
        protected void onProgressUpdate(ProgressAux... params) {
            if(params[0].getTexto() != null)
            mProgressText.setText(params[0].getTexto());

            mProgressBar.setProgress(params[0].getProgresso());
        }

        private String saveImagesIntoInternalStorage(Bitmap bitmapImage, String imgName) {
            ...
        }

        @Override
        protected void onPreExecute() {
            ...
        }

        private void saveChanges() {
            ...
        }

        @Override
        protected Boolean doInBackground(Void... paths) {
            ...
        }

        private AtualizarJSON DeserializaConsultaAtualizacao(String resultadoJSON) throws JSONException {
           ...
        }

        private <T> List<T> DeserializaConsulta(Class<T[]> tipo, String resultadoJSON) throws JSONException {
            ...
        }

        @Override
        protected void onPostExecute(Boolean result) {
           ...
        }

        public interface WSCatalogo91Listener {
            ...
        }
    }

1 个答案:

答案 0 :(得分:1)

您的AsyncTask不应直接操纵UI组件,尤其是在onPostExecuteonProgressUpdate回调之外。由于您已经在使用Activity实现的回调侦听器接口,因此让Activity在回调时操纵它的UI组件会更简单。请确保涵盖生命周期案例,因为AsyncTask不了解Activity生命周期,需要适当取消。