0

AsyncTask を使用して ListView を埋めようとしています。コメントに含まれる問題

private class DownloadDataTask extends AsyncTask<String, Void, Cursor> {
    private final ProgressDialog dialog = new ProgressDialog(ctx);


    @Override
    protected void onPreExecute() {
        this.dialog.setMessage(ctx.getString(R.string.AcquiringData));
        this.dialog.show();
    }


    @Override
    protected Cursor doInBackground(final String... args) {         

        DbAdapter dbAdapter = new DbAdapter(ctx);
        dbAdapter.open();
        // normally when I call this method from main class it reurns cursor full of values
        Cursor cursor = dbAdapter.fetchAllItemsInExpenses();

        return cursor;
    }


    protected void onPostExecute(final Cursor cursor) {
        if (this.dialog.isShowing()) {
            this.dialog.dismiss();
           //cursor is empty

    }

ctxメインクラス OnCreate() に設定されている Context です

fetchAllItemsInExpenses() メソッドを貼り付ける次のリクエスト:

public Cursor fetchAllItemsInExpenses() {
    String query = "SELECT ... " //(some terribly boring and long query string, but trust me, it works perfectly fine)
    return SQLDb.rawQuery(query, null);
}
4

1 に答える 1

0

でこれを試してくださいAsyncTask...

private class DownloadDataTask extends AsyncTask<String, Void, Cursor> {
    private final ProgressDialog dialog = new ProgressDialog(ctx);
    Context ctx = null; //Add this and the constructor below

    public DownloadDataTask(Context context) {
        ctx = context;
    }

    ...

}

次に、次のようにActivity作成して実行AsyncTaskします...

DownloadDataTask ddt = new DownloadDataTask(this);
ddt.execute(theArgs);
于 2012-06-04T18:17:30.647 に答える