0

私のMainActivityには、次のようなクラスの初期化があります。

Presentation presentation = new Presentation("link");

Presentationは、Webサーバー上の.JSONファイルの値を使用して初期化されるクラスです。

public Presentation(String URL) {
    // Do stuff
    doNetworking();
}
private doNetworking() {
    // Network access here
    // This throws the Network on Main Thread exception
}

MainActivityでは、次のステップでPresentationのすべての値が存在する必要があります。

Presentation presentation = new Presentation();
// Do some stuff with it

AsyncTaskを使用して、これをどのように行うべきかわかりません。これまでのところ、次のようなものがあります。public Presentation(String URL){//何か新しいInitializePresentation()。execute(URL); }

private class InitializePresentation extends AsyncTask<String, Void, Boolean> {
    // Amongst other things
    protected Boolean doInBackground(String... params) {
        // Do the networking stuff here
    }
}

私が必要としているのは、このコードをリファクタリングして、非同期であるが同期呼び出しのように動作するようにすることです。どんな助けでも大歓迎です。

編集 これを達成するためにコードをリファクタリングするにはどうすればよいですか?

Bitmap b = new Bitmap();
Load bitmap from network;
Use bitmap in imageview;

これはこのように使用できますか?または私はそれを次のように使用する必要がありますか

Async, doInBackground() {
   Load bitmap from network
   Use bitmap in imageview
   Continue with application
}

ありがとうございました!

4

1 に答える 1

2

ネットワーク関連の作業中に進行状況ダイアログを表示できます。

private ProgressDialog progressDialog;
private Context context;

public InitializePresentation (Context context) {
    this.context = context;
}

@Override
protected void onPreExecute() {
    progressDialog = ProgressDialog.show(context, "", "loading", true);
}

/* 
 * @see android.os.AsyncTask#doInBackground(Params[])
 */
@Override
protected String doInBackground(String... arg0) {
    // Do the networking stuff here
}

@Override
protected void onPostExecute(final String result) {
    progressDialog.dismiss();
}
于 2012-09-26T10:14:47.750 に答える