4

我想在aynctask中更新我的textView,但它根本没有更新。我无法找出我的错误。当我的应用程序失去焦点并重新获得焦点时,UI被更新。奇怪的问题

这里是我的代码:

注: - 在我的应用程序失去焦点和获得焦点的UI更新。奇怪的问题

public class LocateGeopoint extends AsyncTask<LatLng, String, List<Address>> { 

    Geocoder geocoder; 
    private TextView tv; 

    public LocateGeopoint(View v) { 
     this.geocoder = new Geocoder(v.getContext()); 
     this.tv = (TextView) v.findViewById(R.id.textView1); 
    } 

    @Override 
    protected void onPostExecute(List<Address> addresses) { 
     super.onPostExecute(addresses); 
     if(addresses!=null){ 
      String city = addresses.get(0).getAddressLine(0); 
      String country = addresses.get(0).getAddressLine(1); 
      System.out.println(city+" "+country); 

      tv.setText(city); 

     } 
    } 

    @Override 
    protected List<Address> doInBackground(LatLng... params) { 
     // TODO Auto-generated method stub 
     double lattitude = params[0].latitude; 
     double longitude = params[0].longitude; 

     List<Address> addresses = null; 
     if (lattitude != 0 || longitude != 0) { 

      try { 
       addresses = geocoder.getFromLocation(lattitude, longitude, 1); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
     return addresses; 
    } 

} 

XML文件(充气之一)

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/parent_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal" > 

    <ImageView 
     android:id="@+id/imageView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/ic_launcher" /> 

    <FrameLayout 
     android:id="@+id/view_container" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" > 

     <TextView 
      android:id="@+id/textView1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:focusable="true" 
      android:text="Loading Address.." /> 

    </FrameLayout> 

这里是我是如何从主要活动

googleMap.setInfoWindowAdapter(new InfoWindowAdapter() { 

      @Override 
      public View getInfoWindow(Marker arg0) { 
       return null; 
      } 

      @SuppressWarnings("unchecked") 
      @Override 
      public View getInfoContents(Marker marker) { 
       LayoutInflater inflater = (LayoutInflater) MapsDemo.this 
         .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       View v = inflater.inflate(R.layout.custom_info, null); 
       new LocateGeopoint(v).execute(marker.getPosition()); 
       return v; 
      } 
     }); 

调用Asyntask当我调试应用程序,那么它是更新但通常不是。

+0

你有价值的城市? –

+0

也许'geocoder.getFromLocation(lattitude,longitude,1);'只是返回null? –

+0

@MichałZ。它从不返回空值。请仔细阅读该声明。文字未更新。当我锁定屏幕并重新打开它时, - 现在已更新 –

回答

3

添加缓存到您的InfoWindowAdapter代码并修改其getInfoContents()方法来检查:

googleMap.setInfoWindowAdapter(new InfoWindowAdapter() { 
     LruCache<Marker, View> cache = new LruCache<Marker, View>(8); // added 

     // no change to getInfoWindow() 

     @SuppressWarnings("unchecked") 
     @Override 
     public View getInfoContents(Marker marker) { 
      View v = cache.get(marker); 
      if (v == null) { 
       LayoutInflater inflater = (LayoutInflater) MapsDemo.this 
         .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       v = inflater.inflate(R.layout.custom_info, null); 
       new LocateGeopoint(v, marker).execute(marker.getPosition()); 
       cache.put(marker, v); 
      } 
      return v; 
     } 
    }); 

调整代码在你LocateGeopoint类,添加一个参考Marker和更新您的TextView后调用showInfoWindow()

public class LocateGeopoint extends AsyncTask<LatLng, String, List<Address>> { 

    // : 
    Marker marker; // added 

    public LocateGeopoint(View v, Marker marker) { 
     // : 
     this.marker = marker; // added 
    } 

    @Override 
    protected void onPostExecute(List<Address> addresses) { 
     super.onPostExecute(addresses); 
     if(addresses!=null){ 
      // : 
      tv.setText(city); 
      marker.showInfoWindow(); // added 
     } 
    } 

    // no change to doInBackground() 
} 

注意:这个答案是真的只是一个快速和肮脏的实施(我还没有真正测试过,但我认为它应该工作)的基础上,从MaciejGórski对这个问题的答案,请仔细阅读所引用的帖子在这个答案的更多细节:)

+0

它是一个很好的实施,并已为我工作..谢谢@Joe –

+0

但我面临的一个问题是,我得到所有点击前的值..缓存不清理 –

2

可能尝试调用tv.invalidate()希望它会工作

如果没有工作

尝试使类似的onCreate TextView的文本之外的变量;然后在onCreate里面放:text =(TextView)findViewById(R.id.textView2);

然后就把text.setText(city);在onPostExecute方法中。

看看是否有效。

+0

我的Asynctask是一个单独的类,活动类是单独的 –

+0

在活动类本身中添加ur Asynctask。 – flexdroid

+0

为什么我应该添加活动类本身? –

1

试试这个:

runOnUiThread(new Runnable() { 
       public void run() { 

         try { 
          Thread.sleep(25); 
         } catch (InterruptedException e) { 
          e.printStackTrace(); 
         } 
         tv.setText(city); 
       } 
      }); 

在你的代码的仔细检查:

@Override 
protected void onPostExecute(List<Address> addresses) { 
    super.onPostExecute(addresses); 
    if(addresses!=null){ 
     String city = addresses.get(0).getAddressLine(0); 
     String country = addresses.get(0).getAddressLine(1); 
     System.out.println(city+" "+country); 

     tv.setText(city); 

    } 
} 

你重写的onPostExecute()方法,为什么把它叫做是超级功能?请删除super.onPostExecute(addresses);

相关问题