从RunOnUIThread更新视图将一直有效,直到重新启动活动

时间:2016-09-14 19:44:42

标签: java android android-asynctask textview android-runonuithread

我正在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();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

请制作文字视图static