How to populate listView from asyncTask onPostExecute () android

Good afternoon, I'm trying to display a listview using an async task, and I have limited success in displaying the list. the situation is this, I have an activity that is already displayed, and when the user clicks the button, he begins to perform gps actions to get the user's location through an asynchronous task. now I want to display a list containing the results of geocoding so that the user can select the desired location and then update it to a text view in action. I am having trouble displaying this list. it does not appear at all.

here is the corresponding 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; } 

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 
+4
source share
2 answers

I just met the same problem and solved it, in fact you need it:

 adapter.notifyDataSetChanged(); 

and does not need this:

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

AsyncTask works great with the main user interface thread.

+4
source

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

This will not work if you are not calling from the user interface thread.

You will need to do this in your work.

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

Source: https://habr.com/ru/post/1381884/


All Articles