Hy! I'm trying for days now to create an application that receives GPS data and has to store it in a Sqlite database.I've created the database and I also have an GeoPoints Array list that I put in my geopoints.The GPS data is simulated with the help of a KML file. Now,my problem is that even though I succeded to display the map that is updated with the new locations while I try to read from the Sqlite database and display on the screen the geopoints I get nothing-no text displayed on the screen.
Here is my code:
public class screen4 extends MapActivity
{
private LocationManager lm;
private LocationListener locationListener;
private MyLocationOverlay myLocOverlay;
private MapView mapView;
private MapController mc;
List<GeoPoint> geoPointsArray = new ArrayList<GeoPoint>();
int latitude;
int longitude;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen4);
lm = (LocationManager)
getSystemService(Context.LOCATION_SERVICE);
locationListener = new MyLocationListener();
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,locationListener);
mapView = (MapView) findViewById(R.id.mapview1);
mapView.setBuiltInZoomControls(true);
mc = mapView.getController();
initMyLocation();
DBAdapter db=new DBAdapter(this);
db.createDatabase();
db.openDataBase();
for (int i = 0; i < geoPointsArray.size()-1; i++)
{
GeoPoint loc=geoPointsArray.get(i);
db.insertData(Integer.toString(loc.getLongitudeE6()),Integer.toString(loc.getLatitudeE6()),null,null,null);
}
Cursor c=db.getAllData();
if(c.moveToFirst())
{
do{
DisplayTitle(c);
}while(c.moveToNext());
}
if(c!=null&&!c.isClosed()){
db.close();
}
}
private void initMyLocation() {
myLocOverlay = new MyLocationOverlay(this, mapView);
myLocOverlay.enableMyLocation();
mapView.getOverlays().add(myLocOverlay);
}
public void DisplayTitle(Cursor c)
{
Toast.makeText(this,
"longitude: " + c.getString(0) + "\n" +
"latitude: " + c.getString(1) + "\n",
开发者_StackOverflow Toast.LENGTH_LONG).show();
}
private class MyLocationListener implements LocationListener
{
@Override
public void onLocationChanged(Location loc) {
if (loc != null) {
latitude=(int) (loc.getLatitude()* 1E6);
longitude=(int) (loc.getLongitude()* 1E6);
GeoPoint p = new GeoPoint(latitude,longitude);
geoPointsArray.add(p);
mc.animateTo(p);
mc.setZoom(17);
mapView.invalidate();
mapView.setSatellite(true);
}
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
I did debug to my code line by line and as I get to this line(the first one):
for (int i = 0; i < geoPointsArray.size()-1; i++)
{
GeoPoint loc=geoPointsArray.get(i);
db.insertData(Integer.toString(loc.getLongitudeE6()),Integer.toString(loc.getLatitudeE6()),null,null,null);
}
the debugger jumps this snippet...now to me that means that I have no point in my geoPointsArray and I have nothing to store in the data base and there is nothing to read from Sqlite. Can someone help me please with some ideas.Thank you
In general if
guards for arrays should be i < size
and not i < size - 1
.
To avoid mistakes in guards altogether you might use:
for (GeoPoint loc : geoPointsArray) {
....
instead.
Edit - Also, your database access only seems to be performed in onCreate
. You need to do this in your LocationListener
if you expect it to be called when you get updates.
onCreate
is only called once when your activity is created. You should open your database here and then do your accesses when you actually get the data.
精彩评论