1つの画面にリストビューがあり、カスタマイズされたarraylist(List)を使用してこのリストビューをレンダリングします。ここで、別のJavaクラスからリストビューを更新する場合は、コンテキストを使用してリストビューを参照します。配列リストも変更する必要があります。このために、静的修飾子を使用せずに別のJavaクラスから配列リストを変更するにはどうすればよいですか。
質問する
1139 次
2 に答える
1
そのアクティビティの参照 (コンテキスト)を使用してonPostExecute()
、AsyncTask のUI 要素を更新します。
于 2012-07-04T06:55:12.380 に答える
1
これを試して:
...
private Activity context;
onCreate() {
...
context = this;
...
//run async task somewhere
}
class extends AsyncTask<> {
onPostExecute() {
AlertDialog alert = new AlertDialog(context);
// every time you used this on the activity you should use context on any other classes, including async task or other non activity classes
}
以下の@Venkata Krishnaのコメントで述べたように、プロパティを更新するだけの場合、コードは次のようになります。
...
private ArrayList<String> values;
private Activity context;
onCreate() {
...
context = this;
...
//run async task somewhere
}
class extends AsyncTask<> {
private ArrayList<String> values;
onPostExecute() {
AlertDialog alert = new AlertDialog(context);
//notice that there are 2 properties of values with the same name and the same signature
//accessing async task's values
this.values = ...;
//accessing global parameter
values = ...;
// and then he would want to access the list adapter to update the infomation
CustomListAdapter adapter = ((ListView)findViewById(R.id.listview)).getListAdapter();
adapter.notifyDatasetChanged();
}
}
グローバル変数と内部クラス変数を使用した Java プログラミングだけです。通常のオブジェクトの場合、アクティビティの「コンテキスト」は必要ありません。変数を変更するだけです。UI にアクセスしたい場合は、コンテキスト プロパティを保存するというトリックを実行する必要があります。
于 2012-07-04T07:13:21.390 に答える