我怎么能等到完成另一个类的asynctask?

时间:2014-01-21 22:40:13

标签: android android-asynctask

我想等待asynctask完成。

MainActivity.java

public class MainActivity extends Activity {

private List<MyObject> lst_object;
private ObjectManager objectManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initLayout();
    initVariables();

    objectManager = new ObjectManager(this);

    lst_object = objectManager.getAllData();

    **while(!objectManager.getAsyncTaskStatus()) {

    }**

    System.out.println(lst_object.size() + " items");
    }
}

ObjectManager.java

public class ObjectManager {

private Context context;
private Cursor c_object;
private List<MyObject> lst_object;
private MyObject object;

private ProgressDialog progressDialog;
**private boolean status = false;**

public ObjectManager(Context context) {
    this.context = context;
    lst_object = new ArrayList<MyObject>();
}

**public boolean getAsyncTaskStatus() {
    return !status;
}**

class AsyncReadingObjects extends AsyncTask<Void, Integer, String> {
    private int totalObject;
    @Override
    protected void onPreExecute() {
        initProgressBar();
        progressDialog.setMessage("Reading objects");
        System.out.println("onPreExecute finished");
    }

    @Override
    protected String doInBackground(Void... params) {
        System.out.println("doInBackground started");
        lst_object.clear();

        c_object = context.getContentResolver().query(uri_object, null, null,null,"_id ASC");
        totalObject = c_object.getCount();
        try {
            if (c_object.moveToFirst()) {
                for (int i = 0; i < totalObject; i++) {
                    System.out.println(i + ". ITEM");

                    publishProgress(i);
                    c_object.moveToNext();
                }
            }
        } catch(Exception e) {
            e.printStackTrace();
        } finally {
            c_object.close();
        }
        return null;
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        progressDialog.setMessage("Reading objects " + values[0] + "/" + totalObject);
    }

    @Override
    protected void onPostExecute(String values) {
        progressDialog.dismiss();
        **status = true;**
    }
}

public List<Object> getAllData() {
    AsyncReadingObjects asyncReadingObjects = new AsyncReadingObjects();
    asyncReadingObjects.execute();
    return lst_object;
}

这是日志:

01-22 00:02:16.220: I/System.out(31507): 0 items
01-22 00:02:16.230: I/System.out(31507): onPreExecute finished
01-22 00:02:16.230: I/System.out(31507): doInBackground started
01-22 00:02:16.250: I/System.out(31507): 0. ITEM
01-22 00:02:16.250: I/System.out(31507): 1. ITEM
01-22 00:02:16.250: I/System.out(31507): 2. ITEM
01-22 00:02:16.250: I/System.out(31507): 3. ITEM
01-22 00:02:16.250: I/System.out(31507): 4. ITEM
01-22 00:02:16.250: I/System.out(31507): 5. ITEM
01-22 00:02:16.250: I/System.out(31507): 6. ITEM
01-22 00:02:16.250: I/System.out(31507): 7. ITEM
01-22 00:02:16.250: I/System.out(31507): 8. ITEM
01-22 00:02:16.250: I/System.out(31507): 9. ITEM
01-22 00:02:16.250: I/System.out(31507): 10. ITEM
01-22 00:02:16.260: I/System.out(31507): 11. ITEM
01-22 00:02:16.260: I/System.out(31507): 12. ITEM
01-22 00:02:16.260: I/System.out(31507): 13. ITEM
01-22 00:02:16.260: I/System.out(31507): 14. ITEM
....
01-22 00:02:16.260: I/System.out(31507): 300. ITEM

我想做以下

onPreExecute finished
doInBackground started
0. ITEM
1. ITEM
2. ITEM
3. ITEM
4. ITEM
5. ITEM
6. ITEM
....
300. ITEM
300 items

我该怎么办?谢谢你的回答。

1 个答案:

答案 0 :(得分:0)

System.out.println(totalObject) 

在onPostExecute()。

您可能希望向AsyncTask添加一个成功字段,如果它失败(在catch中),您可以设置为false,如果它通过循环,则可以设置为true。

然后在你的onPostExecute()中只打印if(success)