【问题标题】:UI is updated when my app lost focus and regains focus. Strange issue当我的应用失去焦点并重新获得焦点时,UI 会更新。奇怪的问题
【发布时间】:2013-03-22 06:13:59
【问题描述】:

我想在 aynctask 中更新我的 textView,但它根本没有更新。我无法找出我的错误。

这是我的代码:

注意: - 当我的应用失去焦点并重新获得焦点时,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>

这是我从主要活动中调用 Asyntask 的方式

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;
            }
        });

当我调试应用程序时,它会更新但通常不会。

【问题讨论】:

  • 你得到了城市的价值吗?
  • 也许geocoder.getFromLocation(lattitude, longitude, 1); 只是返回null?
  • @MichałZ。它从不返回 null。请仔细阅读声明。文本未更新。当我锁定屏幕并重新打开它时,- 现在它已更新
  • 您在 LogCat 中收到任何消息吗?
  • 不,我没有收到关于 logcat 视图的消息

标签: android android-asynctask textview google-maps-android-api-2


【解决方案1】:

将缓存添加到您的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 对这个问题的回答,请阅读该答案中的参考帖子更多细节:)

【讨论】:

  • 这是一个很好的实现,对我有用.. 谢谢@Joe
  • 但是我面临的一个问题是我在所有点击中都获得了以前的值。缓存没有清理
【解决方案2】:

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

如果这不起作用

尝试在 onCreate 之外创建一个变量,例如 TextView 文本;然后在 onCreate 里面放: text = (TextView) findViewById(R.id.textView2);

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

看看这是否有效。

【讨论】:

  • 我的 Asynctask 是一个单独的类和活动类是单独的
  • 在活动类本身中添加你的 Asynctask。
  • 为什么要自己添加activity类?
  • 访问自己的活动类中的 TextView 实例,并按照我在答案中指定的方式设置文本
  • 不能这样使用吗?你是第一次看吗?
【解决方案3】:

the documentation 中所述,返回的视图不是实时视图,从 getInfoContents 返回后无法更新。

试试这个答案:Dynamic contents in Maps V2 InfoWindow

【讨论】:

    【解决方案4】:

    试试这个:

    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);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-26
      • 1970-01-01
      • 2015-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多