私はより良いハンドルを取得することに取り組んでおりAsyncTask
、asyncTask を使用して動的にコントロールを作成しようとしていますonPostExecute()
。
以下のコードは機能し、コントロールを作成しますが、これをループする方法はありますが、asynctask の完了後に変数 I がインクリメントされるように遅延させる方法はありますか?
get() メソッドを使用して読んだことがありますが、機能させることができないようです。
バックグラウンド タスクが完了するまで待機する方法、または変数に基づいてコントロールを動的に作成する他の方法をアドバイスできる人はいますか?
package com.example.dynamicallycreatecontrols;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;
import android.view.Menu;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
public class MainActivity extends Activity {
Integer i = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
while (i < 5) {
new createControl().execute(i);
i++;
}
}
@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;
}
//asynctask
public class createControl extends AsyncTask<Integer, Void, Button> {
Button btn = new Button(MainActivity.this);
LinearLayout ll = (LinearLayout) findViewById (R.id.llMain);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
protected void onPreExecute(Integer i) {
// nothing right now
}
@Override
protected Button doInBackground(Integer... arg0) {
// TODO Auto-generated method stub
// do the calculation
return null;
}
protected void onPostExecute(Button v) {
// build the controls here
btn.setText("Play" + i);
ll.addView(btn, lp);
SystemClock.sleep(1000);
}
}
}
私はAndroid開発とJavaが初めてなので、get()の概念を誤解しているだけなのか、それともこれをすべて一緒に行うためのより良い方法があるのか わかりません。
お時間を割いてご協力いただきありがとうございます。
-ニック