在我的应用程序中,我有多个标记添加到地图并保存在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;
这最初适用于选择&#34;删除标记&#34;但是当返回活动时,标记会返回并且不会被删除?
我不确定我做错了什么,所以希望有人可以帮助我吗?
答案 0 :(得分:1)
首先,当你从数据库中查询(获取)位置并将它们作为标记添加到地图时,你需要跟踪标记数据库ID,可能ArrayList
你不包括这部分代码,但我想你知道我的意思,
当你添加marker时,它返回marker-id(地图上的id)而不是db-row的id,所以arraylist或hashtable将是db的marker id和row-id之间的映射器。
我会写一些片段,你可以找出插入和使用的位置
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
将此监听器添加到活动的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
现在提供者,我们更改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帮助器。