もともと下位バージョン(2.3)を対象としたAndroid用のアプリケーションを作成しました。概念実証を機能させた後、Android4で機能させようとしました。そのときにNetworkOnMainThread例外が発生しました。
いくつかの調査を行った後、私はすぐにAsyncTaskを見つけました。これは素晴らしいサウンドでした。問題は、頭を悩ませるのに苦労していることです。たとえば、これが私の元のコードです。
public void Refresh(Context c)
{
SummaryModel model = MobileController.FetchSummary(c);
TextView txtCurrentWeight = (TextView)findViewById(R.id.txtCurrentWeight);
TextView txtWeightChange = (TextView)findViewById(R.id.txtWeightChange);
TextView txtAvgPerWeek = (TextView)findViewById(R.id.txtAvgPerWeek);
if(model.ErrorMessage == "")
{
txtCurrentWeight.setText(model.CurrentWeight);
txtWeightChange.setText(model.WeightChange);
txtAvgPerWeek.setText(model.Average);
}
else
{
Toast.makeText(c, model.ErrorMessage, Toast.LENGTH_LONG).show();
txtCurrentWeight.setText("");
txtWeightChange.setText("");
txtAvgPerWeek.setText("");
}
}
私は次のようなAsychTaskを作成しました:
public class WebMethodTask extends AsyncTask<Object, Integer, Object> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(Object result) {
super.onPostExecute(result);
SummaryModel model = (SummaryModel)result;
// Can't seem to access UI items here??
}
@Override
protected Object doInBackground(Object... params) {
Context c = (Context)params[0];
return MobileController.FetchSummary(c);
}
}
onPostExecuteメソッドからUIアイテムにアクセスするにはどうすればよいですか?または、AsyncTaskの使用方法について間違った考えがありますか?
ありがとう!