AsyncTask的问题

时间:2011-05-08 11:16:59

标签: android database android-asynctask

我有以下代码,其中我从数据库(经度,纬度)读取一些数据,并尝试在UI线程中的这些点之间画一条线:

绘制该行的函数是:

public void theRouteDraw(List<GeoPoint> geoPointsArray)
{
    for (int i = 0; i < geoPointsArray.size(); i++)
    {
        p = geoPointsArray.get(i);
        mc.animateTo(p);
        mc.setZoom(13);
        mapView.invalidate();
        mapView.setSatellite(true);
    }
}

private void initMyLocation()
{
    myLocOverlay = new MyLocationOverlay(this, mapView);
    myLocOverlay.enableMyLocation();
    mapView.getOverlays().add(new myLocOverlay());
}

除了这两种方法之外,我的代码是这样的:

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

    db = new DBAdapter(this);
    progress = new ProgressDialog(this);
    progress.setIndeterminate(true);
    progress.setMessage("I am thinking");
    InitTask init_task = new InitTask();
    init_task.execute(db);
    initMap();
}

private void initMap()
{
    mapView = (MapView) findViewById(R.id.mapview);
    mapView.setBuiltInZoomControls(true);
    mapView.setStreetView(true);
    mc = mapView.getController();
}

我的Async类

public class InitTask extends AsyncTask<DBAdapter, GeoPoint, List<GeoPoint>>
{
    List<GeoPoint> geoPointsArray = new ArrayList<GeoPoint>();
    DBAdapter db;
    int latitude;
    int longitude;
    GeoPoint p;

    protected void onPreExecute()
    {
        progress.show();
    }

    protected List<GeoPoint> doInBackground(DBAdapter... db)
    {
        try
        {
            db[0].openDataBase();
            Cursor c = db[0].getAllData();
            if (c.moveToFirst())
            {
                do
                {
                    longitude = Integer.parseInt(c.getString(0));
                    latitude = Integer.parseInt(c.getString(1));
                    p = new GeoPoint(latitude, longitude);
                    geoPointsArray.add(p);
                }
                while (c.moveToNext());
            }
            c.close();
            db[0].close();
        }
        catch (Exception e)
        {
            Log.d("Eroare", "doInBackground", e);
        }

        return geoPointsArray;
    }

    protected void onPostExecute(List<GeoPoint> geoPointsArray)
    {
        // super.onPostExecute(geoPointsArray);
        progress.dismiss();

        Log.d("Lista", new Boolean(geoPointsArray.isEmpty()).toString());
        theRouteDraw(geoPointsArray);
        initMyLocation();
    }
}

问题:

1.为什么我没有在地图上绘制我的路线.....因为我检查并在onPostExecute()我有来自DB的所有点

2.我如何逐步检索这些点....我的意思是检索一些点来更新我的UI并检索其他点并更新我的UI ......?

谢谢你!!

更新

protected List<GeoPoint> doInBackground(DBAdapter... db)
{
    try
    {
        db[0].openDataBase();
        Cursor c = db[0].getAllData();
        if (c.moveToFirst())
        {
            do
            {
                longitude = Integer.parseInt(c.getString(0));
                latitude = Integer.parseInt(c.getString(1));
                p = new GeoPoint(latitude, longitude);
                geoPointsArray.add(p);
                publishProgress(p);
                SystemClock.sleep(1000);
            }
            while (c.moveToNext());
        }
        c.close();
        db[0].close();
    }
    catch (Exception e)
    {
        Log.d("Eroare", "doInBackground", e);
    }
    return geoPointsArray;
}

我使用publishProgress(p);将坐标发送到:

protected void onProgressUpdate(GeoPoint... progress)
{
                 theRouteDraw(progress1[0]);



         int lon=progress1[0].getLongitudeE6();

          int lat=progress1[0].getLatitudeE6();

          GeoPoint p2=new GeoPoint(lon,lat);

        geoPointsArray.add(p2);

         initMyLocation();
}

现在,我显示我让他们一步一步(这是我想要的东西)现在如果我能弄清楚,为什么我没有得到的路线显示我是一个快乐的man.And我一直没很高兴很久.Thx:D


UPDATE2:

class myLocOverlay扩展了Overlay {

    public myLocOverlay(){

    }   

    public void draw(Canvas canvas, MapView mapv, boolean shadow){

    super.draw(canvas, mapv, shadow);

    Projection projection = mapView.getProjection();
    Path p1 = new Path();
   for (int i = 0; i < geoPointsArray.size(); i++) {
   if (i == geoPointsArray.size() - 1) {
       break;
   }
    Point from = new Point();
    Point to = new Point();
   projection.toPixels(geoPointsArray.get(i), from);
   projection.toPixels(geoPointsArray.get(i + 1), to);

    p1.moveTo(from.x, from.y);
    p1.lineTo(to.x, to.y);

    }

    Paint mPaint = new Paint();
    mPaint.setStyle(Style.STROKE);
    mPaint.setColor(Color.GREEN);
    mPaint.setAntiAlias(true);
    mPaint.setStrokeWidth(4);
    canvas.drawPath(p1, mPaint);
    super.draw(canvas, mapView, shadow);

} 


}

在方法initMyLocation()中我将叠加层添加到当前位置.....叠加层是从myLocOverlay()类中获取的......

myLocOverlay()遍历geoArrayPoints并在它们之间划一条线!!!

方法RouteDraw()在地图上放置我得到的新地理输出......并且它工作得很好!

0 个答案:

没有答案