无法成功写入数据库
嘿! 我已经尝试了几天来创建一个接收 GPS 数据并必须将其存储在 Sqlite 数据库中的应用程序。我已经创建了数据库,并且我还有一个放入我的地理点中的 GeoPoints 数组列表。GPS 数据是借助 KML 文件进行模拟。 现在,我的问题是,即使我成功地显示了用新位置更新的地图,当我尝试从 Sqlite 数据库中读取并在屏幕上显示地理点时,我什么也没有得到——屏幕上没有显示任何文本。
这是我的代码:
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",
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;
}
}
我确实逐行调试了我的代码,当我到达这一行(第一行)时:
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);
}
调试器跳转了这个片段......现在对我来说,这意味着我的 geoPointsArray 中没有意义,我数据库中没有任何内容可存储,也没有任何内容可从 Sqlite 中读取。 有人可以帮我一些想法吗?谢谢
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",
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一般来说,数组的
if
保护应该是i
i
i
i
i
i
i
i
i
i
i size
而不是i
大小 - 1。
为了完全避免守卫的错误,你可以使用:
代替。
编辑 - 此外,您的数据库访问似乎仅在
onCreate
中执行。如果您希望在获取更新时调用它,则需要在LocationListener
中执行此操作。onCreate
仅在创建 Activity 时调用一次。您应该在此处打开数据库,然后在实际获取数据时进行访问。In general
if
guards for arrays should bei < size
and noti < size - 1
.To avoid mistakes in guards altogether you might use:
instead.
Edit - Also, your database access only seems to be performed in
onCreate
. You need to do this in yourLocationListener
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.