AsyncTask の onPostExecute メソッドで RunOnUIThread Runnable の setText メソッドを使用して textView を更新しています。
アクティビティを初めて実行すると、textView が更新されます。ただし、別のアクティビティに移動して前のアクティビティに戻ると、UI が更新されなくなります。
デバッガーを使用すると、textView の mText フィールドが新しいテキストに更新されていることがわかります。ただし、コードを実行し続けると、textView はデフォルトのテキストのままになります。
ここで何か不足していますか?なぜこれが必要なのか、私には本当にわかりません。
これが onPostExecute です。フィールドが更新されるメソッドは checkAgainstLocations() です (下記参照)
@Override
protected void onPostExecute(final ArrayList<String> strings) {
super.onPostExecute(strings);
if (strings == null) {
Toast.makeText(ctx, "Please check you have an internet connection", Toast.LENGTH_LONG).show();
return;
}
locationsData = cleanLocations(strings);
if (locationsData.size() < 1) {
locationsData.add("Choose a location...");
} else if (!locationsData.get(0).equals("Choose a location...")) {
locationsData.add(0, "Choose a location...");
}
adaptor.clear();
adaptor.addAll(locationsData);
adaptor.notifyDataSetChanged();
changeLocationButton.setEnabled(true);
if (mCurrentLocation != null) {
checkAgainstLocations();
}
}
これは checkAgainstLocations() メソッドです。
private void checkAgainstLocations() {
Geocoder gcd = new Geocoder(this, Locale.getDefault());
List<Address> addresses = new ArrayList<>();
if (myLocation != null) {
try {
addresses = gcd.getFromLocation(myLocation.getLatitude(), myLocation.getLongitude(), 1);
} catch (IOException e) {
Log.e("loc", "no location yet");
}
}
if (addresses.size() > 0) {
myCity = addresses.get(0).getLocality().toLowerCase();
} else {
myCity = "National";
}
new LoadPromotions().execute(myCity);
if (locationsData.contains(myCity.toLowerCase())) {
if (!userSelected) {
locationsResult = myCity.toLowerCase();
currentLocation.setText(locationsResult);
currentLocation.postInvalidate();
}
}
}