gpt4 book ai didi

android - 项目未从 SQlite 数据库中删除

转载 作者:行者123 更新时间:2023-11-29 01:35:41 24 4
gpt4 key购买 nike

在我的应用程序中,我有多个标记被添加到 map 并保存在 SQLite 中,但现在我想在点击该标记并选择“删除标记”时删除特定标记所以我所做的是:

在我的 LocationsDB.class

@Override
public void onCreate(SQLiteDatabase db) {
String sql = "create table " + DATABASE_TABLE + " ( " +
FIELD_ROW_ID + " integer primary key autoincrement , " +
FIELD_LNG + " double , " +
FIELD_LAT + " double , " +
FIELD_ZOOM + " text , " +
FIELD_TITLE + " text , " +
FIELD_SNIPPET + " text , " +
" ) ";

db.execSQL(sql);
}

public long insert(ContentValues contentValues){
long rowID = mDB.insert(DATABASE_TABLE, null, contentValues);
return rowID;
}

public int del(int id){
int cnt = mDB.delete(DATABASE_TABLE, FIELD_ROW_ID+"="+id, null);
return cnt;
}

然后在我的 LocationsContentProvider.class 中:

 public class LocationsContentProvider extends ContentProvider{

public static final String PROVIDER_NAME = "com.maps.locationsmaps.locations";
public static final Uri CONTENT_URI = Uri.parse("content://" + PROVIDER_NAME + "/locations" );
private static final int LOCATIONS = 1;
private static final UriMatcher uriMatcher ;

static {
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
uriMatcher.addURI(PROVIDER_NAME, "locations", LOCATIONS);
}

LocationsDB mLocationsDB;

@Override
public boolean onCreate() {
mLocationsDB = new LocationsDB(getContext());
return true;
}

@Override
public Uri insert(Uri uri, ContentValues values) {
long rowID = mLocationsDB.insert(values);
Uri _uri=null;
if(rowID>0){
_uri = ContentUris.withAppendedId(CONTENT_URI, rowID);
}else {
try {
throw new SQLException("Failed to insert : " + uri);
} catch (SQLException e) {
e.printStackTrace();
}
}
return _uri;
}

@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
return 0;
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
int cnt = 1;
cnt = mLocationsDB.del(cnt);
return cnt;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {

if(uriMatcher.match(uri)==LOCATIONS){
return mLocationsDB.getAllLocations();
}
return null;
}

@Override
public String getType(Uri uri) {
return null;
}
}

最后在我的 MainActivity.class 中:

public void onClick(DialogInterface arg0, int arg1) {
marker.remove();
LocationDeleteTask deleteTask = new LocationDeleteTask();
deleteTask.execute();
}
})

....

@Override
public void onLoaderReset(Loader<Cursor> arg0) {
}
class LocationInsertTask extends AsyncTask<ContentValues, Void, Void>{
@Override
protected Void doInBackground(ContentValues... contentValues) {
getContentResolver().insert(LocationsContentProvider.CONTENT_URI, contentValues[0]);
return null;
}
}

private class LocationDeleteTask extends AsyncTask<Void, Void, Void>{

@Override
protected Void doInBackground(Void... params) {
getContentResolver().delete(LocationsContentProvider.CONTENT_URI, null, null);
return null;

这最初在选择“删除标记”时起作用,但是当返回到 Activity 时,标记随后返回并且没有被删除?

我不确定我做错了什么,所以希望有人能帮助我吗?

最佳答案

首先,当您从数据库查询(获取)位置并将它们作为标记添加到 map 时,您需要跟踪标记数据库 ID,也许 ArrayList 您没有包括这部分的代码,但我假设你知道我的意思,

当您添加标记时,它会返回标记 ID( map 上的 ID)而不是 db-row 的 ID,因此数组列表或哈希表将成为标记 ID 和数据库行 ID 之间的映射器。

我会写一些 fragment ,你可以想出在哪里插入和使用

Hashtable <Integer, String> mapid_dbid = new Hashtable <Integer, String>()
private void populateMap(){
while(some cursor loop){
Marker marker = new Marker(... init from DB);
int mapid = map.addMarker(marker);
//mapid her is an ID generated for the marker once it's added to the map
mapid_dbid.put(mapid, cursor.getString("row_id"));
}
}

现在我们转到 onClick(),您需要使用类 OnMarkerClickListener 中的 onMarkerClick

将此监听器添加到 Activity 的 mapview(可能在 onCreate() )。

@override
public boolean onMarkerClick (Marker marker){
marker.remove();
LocationDeleteTask deleteTask = new LocationDeleteTask();
deleteTask.execute(marker.getId());
}

现在我们修改 DeleteTask:(将 1st Void 更改为 Integer)它是 in-params 类型。

private class LocationDeleteTask extends AsyncTask<Integer, Void, Void>{

@Override
protected Void doInBackground(Integer... params) {//note Void changed to Integer too
//paramsnow params might be 1 or more, so in this case we sent 1 param, so we read it as params[0]
//and it's the markers Map-id, we need to get the db-id from the hashtable.
String dbId = mapid_dbid.get(params[0]);

getContentResolver().delete(LocationsContentProvider.CONTENT_URI, null, new String[]{dbId});//pass dbid of the location to the delete, so it can be passed to db-delete
return null;
}
}//Task

现在是 Provider,我们更改 delete() 方法:

@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
int cnt = 1;
cnt = mLocationsDB.del(selectionArgs[0]);
return cnt;
}

我没有你所有的代码,所以你必须尝试找出你应该做什么使其适合您的代码,

主要思想,是获取标记 ID(来自 map )并使用它来获取位置的 db-id然后将其传递给 deleteTask,后者会将其传递给 Provider,提供者会将其传递给 sqlite 帮助器。

关于android - 项目未从 SQlite 数据库中删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28260162/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com