irobotxx
irobotxx

Reputation: 6063

how to Populate a listView from an asyncTask onPostExecute() android

Good day, I am trying to display a listview via async task and am having limited success in the listview showing up. the situation is this, i have an activity that is already displayed and when the user presses a button, it starts performing gps activities to get the users location through an async task. now i want to display a listview containing the results of the geocoder so that the user can choose the location he wants and then it updates back to a textview in the activity. am having trouble displaying this listview. it does not show at all.

here is the relevant code:

public void onCreate(Bundle savedInstanceState){
     super.onCreate(savedInstanceState);
    setContentView(R.layout.view_location);

}

Async Task class:

class locationTask extends AsyncTask<Object, Void, List<Address> > {
        List<Address> addresses;
        ProgressDialog progressDialog;
        Activity location_activity;

        public locationTask(Activity activity){
            location_activity = activity;
        }

        protected void onPreExecute(){

            progressDialog = ProgressDialog.show(LocationViewer.this, "", "Getting GPS information Location here");
        }

        protected List<Address> doInBackground(Object... params){
            Log.d(TAG, "am in doinbackground now");
            addresses = doGeoCode(globalLocation);

            int count = addresses.size();
            Log.d(TAG, "count in doinbackground = " + count);
            Log.d(TAG, "am out of doinbackground now");
            return null;
        }


        @Override
        protected void onPostExecute(List<Address> address){
            progressDialog.dismiss();
            int count = addresses.size();
            Log.d(TAG, "count = " + count);
            Log.d(TAG, "am in PostExecute now");
            locationAdapter adapter = new locationAdapter(LocationViewer.this, android.R.layout.simple_list_item_1, addresses);
            adapter.notifyDataSetChanged();
            list.setAdapter(adapter);



        }
    }

doGeocode method:

public List<Address> doGeoCode(Location location){

    globalLocation = location;

    double newlatitude = globalLocation.getLatitude();
    double newlongitude =globalLocation.getLongitude();

    Geocoder geocode = new Geocoder(this, Locale.getDefault());
    try{
        addresses = geocode.getFromLocation(newlatitude, newlongitude, 1);
    } catch(IOException e){}

    return addresses;
    //return addresses;
}

The adapter class:

class locationAdapter extends ArrayAdapter<Address> {
        Context mycontext;

        public locationAdapter(Context context,int textViewResourceId, List<Address> addresses) {
            super(context,textViewResourceId, addresses);

            mycontext = context;
        }

        @Override   
        public View getView(int position, View convertView, ViewGroup parent){

            /*View row = convertView;

            if(row == null){
                LayoutInflater inflater = getLayoutInflater();
                row = inflater.inflate(R.layout.view_list, parent, false);
            }*/

        int maxAddressLineIndex = getItem(position).getMaxAddressLineIndex();
            String addressline = "";

            for(int j=0; j <= maxAddressLineIndex; j++){
                addressline += getItem(position).getAddressLine(j) + ",";
            }

            TextView rowAddress = new TextView(mycontext);
            rowAddress.setText(addressline);

            return rowAddress;
        }

    }



public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        StringBuilder sb = new StringBuilder();

        sb.append(((Address)parent.getItemAtPosition(position)).getAddressLine(position)).append(",");
        sb.append(((Address)parent.getItemAtPosition(position)).getAdminArea()).append(";");
        sb.append(((Address)parent.getItemAtPosition(position)).getPostalCode()).append(";");
        sb.append(((Address)parent.getItemAtPosition(position)).getCountryName());

        address = sb.toString();
    }

logcat:

11-18 11:47:18.538: VERBOSE/LocationManagerService(1350): requestLocationUpdates

11-18 11:47:18.548: DEBUG/WifiService(1350): enable and start wifi due to updateWifiState

11-18 11:47:18.588: INFO/System.out(1459): [INFO:467227473]: LogSource: Running flush

11-18 11:47:18.588: INFO/System.out(1459): [INFO:467227475]: LogSource: Sending payload [bytes=247]

11-18 11:47:18.648: INFO/System.out(1459): [INFO:467227537]: LogSource: Response [http=200,length=219]

11-18 11:47:18.648: INFO/System.out(1459): [INFO:467227538]: LogSource: Read id 119, status code 200

11-18 11:47:18.688: DEBUG/InputManagerService(1350): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@408a9630

11-18 11:47:18.858: DEBUG/SurfaceFlinger(1350): layer=0xb8fdb8 is not in the purgatory list

11-1811:47:19.529: INFO/wpa_supplicant(1548): got scan complete 

11-18 11:47:19.529: INFO/wpa_supplicant(1548): wpa_supplicant_get_scan_results:return scan results2

11-18 11:47:19.529: INFO/wpa_supplicant(1548): AP:ssid[ICYSPICY],rssi[-53],BSSID=00:1c:df:73:b2:6c

11-18 11:47:19.529: INFO/wpa_supplicant(1548): AP:ssid[virginmedia2196134],rssi[-91],BSSID=c4:3d:c7:41:12:f3

11-18 11:47:19.529: INFO/wpa_supplicant(1548): AP:ssid[ICYSPICY],rssi[-85],BSSID=00:24:b2:b4:8b:f8

11-18 11:47:19.529: INFO/wpa_supplicant(1548): Received 950 bytes of scan results (3 BSSes)

11-18 11:47:19.529: INFO/wpa_supplicant(1548): wpa_driver_wext_get_scan_results---

11-18 11:47:19.549: DEBUG/GpsLocationProvider(1350): GetGpsInterface+

11-18 11:47:19.549: DEBUG/GpsLocationProvider(1350): GetGpsInterface-

11-18 11:47:19.549: DEBUG/lib_locapi(1350): loc_eng_inject_location, accuracy = 56.0

11-18 11:47:19.569: DEBUG/AutoSetting(2277): service - mLocationListener: onLocationChanged() location = Location[mProvider=network,mTime=1321616839556,mInfo=-22.2383714333463.40099234999997,mAccuracy=56.0

11-18 11:47:19.569: DEBUG/AutoSetting(2277): service - handleMessage() incoming message, what:1

11-18 11:47:19.569: DEBUG/AutoSetting(2277): service - mLocationListener: onLocationChanged() location = Location[mProvider=network,mTime=1321616839556,mInfo=-22.2383714333463.40099234999997,mAccuracy=56.0

11-18 11:47:19.569: DEBUG/AutoSetting(2277): Util - isSetupWizardCompleted(): true

11-18 11:47:19.569: DEBUG/AutoSetting(2277): Util - wifi connected

11-18 11:47:19.569: DEBUG/AutoSetting(2277): service - processLocationBundle() distance to current is less than 1000.0m, bypass update

11-18 11:47:19.569: DEBUG/AutoSetting(2277): service - handleMessage() within range1

Upvotes: 4

Views: 15887

Answers (2)

Jordy Langen
Jordy Langen

Reputation: 3591

In

@Override
    protected void onPostExecute(List<Address> address){
        progressDialog.dismiss();
        locationAdapter adapter = new locationAdapter(LocationViewer.this, android.R.layout.simple_list_item_1, addresses);
        list.setAdapter(adapter);
    }

call

adapter.notifyDataSetChanged();

That won't work if you are not calling from the UI thread though.

You would need to do this in you Activity

runOnUiThread(new Runnable(){
        @Override
        public void run(){
            adapter.notifyDataSetChanged();
        }
    });

Upvotes: 2

acoustic
acoustic

Reputation: 4577

I just met the same issue and solved it, actually you just need this:

adapter.notifyDataSetChanged();

and don't need this:

runOnUIThread(new Runable(){

    @Override
    public void run(){
        adapter.notifyDataSetChanged();
    }
};

The AsyncTask works fine with the main UI Thread.

Upvotes: 4

Related Questions