I have a map. I have a database of points. I want to display points from database in AsyncTask, so user can move around the map, when the points are being loaded. There's not much points (like 353 or etc), but I redraw them all when user gets closer (zoom) to the map with bigger markers. When this redraw occurs, app freezes like for a fraction of a second, but it's noticeable, so I thought AsyncTask would be the answer.
However it's not working. And it's weird.
I found a few questions here, but the problem there was mostly not calling the "publishProgress()" method, which is not my case.
In the method "doInBackground()" I open the database, do the query and save it to a Cursor (which is attribute of my ShowPoints class, which extends AsyncTask, so it shoud be accesable by all methods of this class //at least I think that).
Than I go through the cursor and each time call "publishProgress()" which shoud call "onProgressUpdate()" and here I create the marker using my cursor from "doInBackgroud()".
It's strange that if I do debug, and go through the code by step, it all works as it shoud. But when I run the app, no markers are added or only a few of them.
private class ShowPoints extends AsyncTask<String, Integer, String> {
private String resp="Body načteny";
private Cursor c_body;
private LatLng marker_pozice;
@Override
protected String doInBackground(String... params) {
int i=0;
try {
DatabaseHelper db = new DatabaseHelper(MainActivity.this);
try {
db.openDataBase();
}catch(SQLException sqle){
throw sqle;
}
c_body = db.query(DatabaseHelper.TABLE_POINTS, allColumns, null, null, null,null, null);
if(c_body.moveToFirst())
{
if(params[0]=="small"){
do {
i++;
int ikona = R.drawable.gray_dot;
int trasa = c_body.getInt(6);
switch(trasa){
case 1: ikona = R.drawable.color_00a750_dot;
break;
case 2: ikona = R.drawable.color_ec1c24_dot;
break;
case 3: ikona = R.drawable.color_ffca05_dot;
break;
case 4: ikona = R.drawable.color_6dcef5_dot;
break;
case 5: ikona = R.drawable.color_f497b0_dot;
break;
case 6: ikona = R.drawable.color_0088cf_dot;
break;
case 7: ikona = R.drawable.color_98d3bf_dot;
break;
case 8: ikona = R.drawable.color_c3792f_dot;
break;
case 9: ikona = R.drawable.color_c5168c_dot;
break;
}
publishProgress(ikona);
Log.i("Thread",String.valueOf(i));
} while (c_body.moveToNext());
}else{
do {
i++;
int ikona = R.drawable.bigg_dot;
int trasa = c_body.getInt(6);
switch(trasa){
case 1: ikona = R.drawable.biggr_00a750_dot;
break;
case 2: ikona = R.drawable.biggr_ec1c24_dot;
break;
case 3: ikona = R.drawable.biggr_ffca05_dot;
break;
case 4: ikona = R.drawable.biggr_6dcef5_dot;
break;
case 5: ikona = R.drawable.biggr_f497b0_dot;
break;
case 6: ikona = R.drawable.biggr_0088cf_dot;
break;
case 7: ikona = R.drawable.biggr_98d3bf_dot;
break;
case 8: ikona = R.drawable.biggr_c3792f_dot;
break;
case 9: ikona = R.drawable.biggr_c5168c_dot;
break;
}
Log.i("Thread",String.valueOf(i));
publishProgress(ikona);
} while (c_body.moveToNext());
}
}
db.close();
} catch (Exception e) {
e.printStackTrace();
resp = e.getMessage();
}
return resp;
}
@Override
protected void onProgressUpdate(Integer... ikona) {
marker_pozice = new LatLng(c_body.getDouble(3), c_body.getDouble(4));
mMap.addMarker(new MarkerOptions()
.position(marker_pozice)
.title(c_body.getString(2))
.snippet(c_body.getString(7)+"\n"+c_body.getString(1))
.icon(BitmapDescriptorFactory.fromResource(ikona[0])));
}
// progress. For example updating ProgessDialog
}
I call this in the onCreate method:
ShowPoints pointShower = new ShowPoints();
pointShower.execute("small");
Also the logging I'm doing in "doInBackgroud()" works, but markers are not added to the map (or not all of them ... sometimes there are drawn 3, sometimes 19 etc).
mMap is accesable, because this class is inside another class, where the map is an attribute and is instantiated before this Async is called. Also as I'm saying, in debuger step by step I can draw all the points.
Also in "onPostExecute()" I have a toast, which is displayed when I launch the app, so there's no doubt it finishes the async (logging works, final method is called).