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