5

I want to update my textView in aynctask, but it is not updating at all. I am unable to find out my error.

Here is my code :

NOTE : - UI is updated when my app lost focus and regains focus. Strange problem

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 file (Inflated one )

<?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>

Here is how I am calling the Asyntask from main activity

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

When I debug the application, then it is updated but normally it is not.

Gaurav Arora
  • 8,282
  • 21
  • 88
  • 143

4 Answers4

3

Add a cache to your InfoWindowAdapter code and modify its getInfoContents() method to check it:

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

Adjust the code in your LocateGeopoint class, adding a reference to the Marker and calling showInfoWindow() after updating your TextView.

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

NOTE: This answer is really just a quick and dirty implementation (I haven't really tested it, but I think it should work) based on the answer from MaciejGórski on this question, please read the referenced post in that answer for more details :)

Joe
  • 14,039
  • 2
  • 39
  • 49
2

May be try calling tv.invalidate() hope it will work

if this didnt work

try making a variable outside the onCreate like TextView text; and then inside the onCreate put: text = (TextView) findViewById(R.id.textView2);

and then just put text.setText(city); inside the onPostExecute method.

See if that works.

flexdroid
  • 8,437
  • 2
  • 21
  • 27
1

As stated in the documentation, View returned is not a live view and cannot be updated after being returned from getInfoContents.

Try following this answer: Dynamic contents in Maps V2 InfoWindow

Community
  • 1
  • 1
MaciejGórski
  • 22,187
  • 7
  • 70
  • 94
1

Try this:

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

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

Upon closer inspection of your code:

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

    }
}

You've overriden the onPostExecute() method, why call it's super function? Please remove super.onPostExecute(addresses);.

Karthik Balakrishnan
  • 4,353
  • 6
  • 37
  • 69