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

时间:2013-04-01 06:47:00

标签: android android-asynctask textview 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;
    }

}

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

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

4 个答案:

答案 0 :(得分: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课程中的代码,在更新TextView后添加对Marker的引用并调用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关于这个问题的答案,请阅读该答案中的引用帖子更多细节:)

答案 1 :(得分:2)

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

如果这不起作用

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

然后只需输入text.setText(city);在onPostExecute方法中。

看看是否有效。

答案 2 :(得分:1)

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

请尝试以下答案:Dynamic contents in Maps V2 InfoWindow

答案 3 :(得分: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);