0

我正在使用它的setText方法在AsyncTask的onPostExecute方法的RunOnUIThread Runnable中更新textView。从RunOnUIThread更新视图,直到活动重新启动

我第一次运行活动时,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(); 
     } 
    } 
} 

回答

0

请让你的textview static

+0

谢谢图沙尔,这的确是问题所在。不敢相信我自己没有想到这件事。 – Cross2four