2017-02-09 82 views
0

我在我的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

+0

你的数据库类中有删除行方法吗? –

+0

不知道亚当。你能检查一下吗,我是SQL新手,非常感谢! – TSM

回答

0

游标是临时对象,并且不应该长一段时间保持左右。

无论如何,游标中的位置不一定与列表中的位置相同,尤其是在进行了更改或进行排序/过滤之后。 将ID存储在列表中,并用它稍后访问该行。

+0

非常感谢,但经过一些重构后,它无法工作。我添加了一个自定义对象,它保存了SQL的名称,位置和标识,但是当我删除它时,它仍然不起作用。你可以看看我的SQL和MainActivity类 - 方法“removeSQLLocation”。在我的github https://github.com/tsmadm/Stormy-WeatherApp/tree/SQL/app/src/main/java/com/tsm/stormy/SQL – TSM

+0

此外,当我重新启动应用程序,它将删除位置.. 。 – TSM