I am trying to convert a method in my app to an async method. It is a method which loads a number of custom markers on Google Map view. I need to make this async so that the view displays instantly while the markers then load.
Can anyone help give me some pointers on how I can convert the below method to an async method ? I am struggling to understand the concept. Thank you.
private void showpins() throws IOException {
SQLiteDatabase db = mDbHelper.getReadableDatabase();
Cursor mCursor = db.query(TABLE_NAME, null, null, null, null, null, null);
startManagingCursor(mCursor);
while (mCursor.moveToNext()) {
Address = mCursor.getString(4);
Name = mCursor.getString(0);
String noSpaces = Address.replaceAll(" ", "+");
JSONObject geocoded = getLocationInfo(noSpaces);
GeoPoint point = getGeoPoint(geocoded);
List<Overlay> mapOverlays = mapView.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.pushpin);
CustomizedItemOverlay itemizedOverlay =
new CustomizedItemOverlay(drawable, this);
OverlayItem overlayitem =
new OverlayItem(point, Name, Address);
itemizedOverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedOverlay);
}
}
I’m not sure what all the methods do that are called by the method you posted, but I’ll assume that only the calls to mapOverlays affects the UI and that everything else can happen in the background. You can turn this into an AsyncTask like this (this would be best as an inner class, I think):