あなたがデータを分離し、もう少しを表示したい場合は、リスナーのパターンに従うことができます:
あなたの活動は、新しいインターフェイスを実装してください - のようなもの:
public interface boolean OnTextUpdatedListener {
onTextUpdated(int id, String newText);
}
と実装( )あなたの活動に:
@Override
public boolean onTextUpdated(int id, String newText) {
TextView tv = (TextView)findViewById(id);
if(tv == null) {
return false;
}
tv.setText(newText);
return true;
}
そして、このようになりますAsyncTask:
public class TextUpdater extends AsyncTask<Void, String, Void> {
OnTextUpdatedListener mListener;
int mId;
volatile boolean mRunning;
public TextUpdater(OnTextUpdatedListener listener, int id) {
super();
mListener = listener;
mId = id;
}
@Override
protected void onProgressUpdate(String... values) {
mRunning = mListener.onTextUpdated(mId, values[0]);
}
@Override
protected Void doInBackground(Void... params) {
mRunning = true;
while(mRunning) {
//retrieve text from network
this.publishProgress(downloadedText);
}
return null;
}
@Override
protected void onPostExecute(Void result) {
mListener = null;
}
}
アクティビティはUIを制御し、AsyncTaskをダウンロードし、何かが更新されたときに通知します。
"...もっと良い方法がありますか?"この方法で何が問題になりますか? –
UIの操作をデータ処理と分離して、より良い方法ではないのですか?モジュラープログラミングをしようとするように。 – mthpvg