当我的应用程序失去焦点并重新获得焦点时,UI 会更新。奇怪的问题

Gau*_*ora 5 android textview android-asynctask google-maps-android-api-2

我想更新 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;
    }

}
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

这是我从主要活动中调用 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;
            }
        });
Run Code Online (Sandbox Code Playgroud)

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

Joe*_*Joe 3

在代码中添加缓存InfoWindowAdapter并修改其getInfoContents()方法来检查它:

\n\n
    googleMap.setInfoWindowAdapter(new InfoWindowAdapter() {\n        LruCache<Marker, View> cache = new LruCache<Marker, View>(8); // added\n\n        // no change to getInfoWindow()\n\n        @SuppressWarnings("unchecked")\n        @Override\n        public View getInfoContents(Marker marker) {\n            View v = cache.get(marker);\n            if (v == null) {\n                LayoutInflater inflater = (LayoutInflater) MapsDemo.this\n                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);\n                v =  inflater.inflate(R.layout.custom_info, null);\n                new LocateGeopoint(v, marker).execute(marker.getPosition());\n                cache.put(marker, v);\n            }\n            return v;\n        }\n    });\n
Run Code Online (Sandbox Code Playgroud)\n\n

调整类中的代码LocateGeopoint,添加对 的引用并在更新 TextView 后Marker调用。showInfoWindow()

\n\n
public class LocateGeopoint extends AsyncTask<LatLng, String, List<Address>> {\n\n    // :\n    Marker marker; // added\n\n    public LocateGeopoint(View v, Marker marker) {\n        // :\n        this.marker = marker; // added\n    }\n\n    @Override\n    protected void onPostExecute(List<Address> addresses) {\n        super.onPostExecute(addresses);\n        if(addresses!=null){\n            // :\n            tv.setText(city);\n            marker.showInfoWindow(); // added\n        }\n    }\n\n    // no change to doInBackground()\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

注意:这个答案实际上只是一个快速而肮脏的实现(我没有真正测试过它,但我认为它应该有效),基于 MaciejG\xc3\xb3rski 关于这个问题的答案,请阅读其中引用的帖子回答以获取更多详细信息:)

\n