我在我的Android应用程序中使用SQLite。所有的都很棒,但是当我在我的天气应用程序中添加一个位置然后将它滑开时,它不会从TABLE中删除位置。Android上的SQLite不删除行
在这里我得到的位置,添加位置并将其删除。
public TreeMap<String, LatLng> getSQLLocations() {
TreeMap<String, LatLng> locations = new TreeMap<>();
for (int i =0; i < mCursor.getCount(); i++) {
mCursor.moveToPosition(i);
String name = mCursor.getString(mCursor.getColumnIndex(WeatherContract.WeatherEntry.COLUMN_LOCATION_NAME));
double lat = mCursor.getDouble(mCursor.getColumnIndex(WeatherContract.WeatherEntry.COLUMN_LAT));
double lon = mCursor.getDouble(mCursor.getColumnIndex(WeatherContract.WeatherEntry.COLUMN_LONG));
LatLng latLng = new LatLng(lat, lon);
locations.put(name, latLng);
}
return locations;
}
public boolean removeSQLLocation(int position) {
mCursor.moveToPosition(position);
long id = mCursor.getLong(mCursor.getColumnIndex(WeatherContract.WeatherEntry._ID));
boolean deleted = mDb.delete(WeatherContract.WeatherEntry.TABLE_NAME, WeatherContract.WeatherEntry._ID + "="
+ id, null) > 0;
return deleted;
}
public long addSQLLocation(String name, LatLng latLng) {
double lat = latLng.latitude;
double lon = latLng.longitude;
ContentValues cv = new ContentValues();
cv.put(WeatherContract.WeatherEntry.COLUMN_LOCATION_NAME, name);
cv.put(WeatherContract.WeatherEntry.COLUMN_LAT, lat);
cv.put(WeatherContract.WeatherEntry.COLUMN_LONG, lon);
long ret = mDb.insert(WeatherContract.WeatherEntry.TABLE_NAME, null, cv);
refreshLocationNames();
return ret;
}
编辑1:所以这是问题所在。我尝试删除该行,但是当我直接从数据库重新加载数据时,它不刷新。所以当我将数据添加到数据库并尝试重新加载时,它也不会刷新。问题在于它不会刷新数据库更改,但它会在重新启动应用程序时刷新。
这里是我的github上的数据库文件链接:
https://github.com/tsmadm/Stormy-WeatherApp/tree/SQL/app/src/main/java/com/tsm/stormy/SQL
你的数据库类中有删除行方法吗? –
不知道亚当。你能检查一下吗,我是SQL新手,非常感谢! – TSM