1

我搜索了其他帖子,但没有找到任何描述我的特定问题的帖子。其中许多是指从 doInBackground 调用 publishProgress。我正在从 onPreExecute 调用 publishProgress,期望显示一个对话框以通知用户正在获取数据。AsyncTask 正在从用户从 Spinner 中进行选择启动。UI 不会显示对话框,而是冻结直到 AsyncTask 完成。我已在日志消息中添加,看起来虽然在 onPreExecute 中调用了 publishProgress,但直到 doInBackground 完成后才会执行 onProgressUpdate。

活动:

public class MainActivity extends Activity implements OnItemSelectedListener{

private Spinner spinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

}



@Override
protected void onResume() {
    super.onResume();

    spinner = (Spinner)findViewById(R.id.spinner);
    SpinnerAdapter adapter = new CustomSpinnerAdapter<String>(this, new String[]{"one", "two", "three"});
    spinner.setAdapter(adapter);
    spinner.setOnItemSelectedListener(this);

}



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@SuppressLint("NewApi")
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    try{
        FetchDataTask fetchDataTask = new FetchDataTask();
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
            fetchDataTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, "data");
        } else{
            fetchDataTask.execute("data");
        }
        fetchDataTask.await();
    } catch (InterruptedException e){
        e.printStackTrace();
    }
}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub

}

public class FetchDataTask extends AsyncTask<String, Integer, Boolean> {

    private  final String TAG = FetchDataTask.class.getSimpleName();
    private  final Integer FETCHING_DATA = 1;

    CountDownLatch countDownLatch = new CountDownLatch(1);
    ProgressDialog dialog;

    @Override
    protected void onPostExecute(Boolean result) {
        super.onPostExecute(result);
        Log.d(TAG,"Exiting onPostExecute");     
        dialog.dismiss();       
    }

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
        publishProgress(FETCHING_DATA);
        Log.d(TAG, "Exiting onPreExecute");
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        super.onProgressUpdate(values);
        dialog = ProgressDialog.show(MainActivity.this, "Fetching", "Data");
        Log.d(TAG, "Exiting onProgressUpdate");

    }

    @Override
    protected Boolean doInBackground(String... arg0) {

        /*
         *  Fetching data here
         */

        Log.d(TAG, "Exiting doInBackground");
        countDownLatch.countDown();
        return true;
    }

    public void await() throws InterruptedException{
        countDownLatch.await();
    }

}
}

微调器:

public class CustomSpinnerAdapter<T> implements SpinnerAdapter {

Context context;
T[] values;

public CustomSpinnerAdapter(Context context, T[] values){
    this.context = context;
    this.values = values;
    for(T value: this.values){
        Log.d("Spinner", value.toString());
    }
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return values.length;
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return values[position];
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public int getItemViewType(int position) {
    // TODO Auto-generated method stub
    return android.R.layout.simple_spinner_dropdown_item;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    TextView v = new TextView(context);
    v.setTextColor(Color.BLACK);
    v.setText(values[position].toString());
    return v;
}

@Override
public int getViewTypeCount() {
    // TODO Auto-generated method stub
    return 1;
}

@Override
public boolean hasStableIds() {
    // TODO Auto-generated method stub
    return false;
}

@Override
public boolean isEmpty() {
    // TODO Auto-generated method stub
    return false;
}

@Override
public void registerDataSetObserver(DataSetObserver observer) {
    // TODO Auto-generated method stub

}

@Override
public void unregisterDataSetObserver(DataSetObserver observer) {
    // TODO Auto-generated method stub

}

@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    return this.getView(position, convertView, parent);
}

}

风景:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<Spinner
    android:id="@+id/spinner"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
     />

</RelativeLayout>

我认为这可能与从 Spinner 选择中执行 AsyncTask 有关,因为只需在 Activity 中执行它而不将其连接到 Spinner 就没有这个问题。任何援助将不胜感激。

4

1 回答 1

0

您不会继续显示有关进度更新的对话框,而是显示它onPreExecute更新它onProgressUpdate并隐藏它onPostExecutecancel. 在您的情况下,您不需要发布进度,因为 progressBar 是不确定的

    @Override
    protected void onPreExecute()
    {
        super.onPreExecute();
        Log.d(TAG, "Exiting onPreExecute");
        dialog = ProgressDialog.show(MainActivity.this, "Fetching", "Data");
    }

    @Override
    protected Boolean doInBackground(String... arg0)
    {
        Log.d(TAG, "Exiting doInBackground");
        countDownLatch.countDown();
        //publishProgress(x);
        return true;
    }

    public void await() throws InterruptedException
    {
        countDownLatch.await();
    }

    @Override
    protected void onProgressUpdate(Integer... values)
    {
        super.onProgressUpdate(values);
        Log.d(TAG, "Exiting onProgressUpdate");
        // update progressbar value
        // ex progresssBar.setProgress(values[0]);

    }
    @Override
    protected void onPostExecute(Boolean result)
    {
        super.onPostExecute(result);
        Log.d(TAG,"Exiting onPostExecute");     
        dialog.dismiss();       
    }
于 2013-10-21T07:09:45.010 回答