从 RunOnUIThread 更新视图一直有效,直到 activity 重新启动
Updating a view from RunOnUIThread works until activity is restarted
我正在 RunOnUIThread Runnable 中的 AsyncTask 的 onPostExecute 方法中用它的 setText 方法更新 textView。
我第一次 运行 activity,textView 更新。但是,当我转到另一个 activity 和 return 到前一个 activity 时,它不再更新 UI.
使用调试器我可以看到 textView 的 mText 字段正在更新为新文本。但是,当我继续 运行ning 代码时,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();
}
}
}
请制作您的文本视图static
。
我正在 RunOnUIThread Runnable 中的 AsyncTask 的 onPostExecute 方法中用它的 setText 方法更新 textView。
我第一次 运行 activity,textView 更新。但是,当我转到另一个 activity 和 return 到前一个 activity 时,它不再更新 UI.
使用调试器我可以看到 textView 的 mText 字段正在更新为新文本。但是,当我继续 运行ning 代码时,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();
}
}
}
请制作您的文本视图static
。