gpt4 book ai didi

android - "getContentResolver().delete()"删除每一行而不是只删除一行

转载 作者:搜寻专家 更新时间:2023-10-30 23:47:32 24 4
gpt4 key购买 nike

我正在尝试制作自己的 ContentProvider,目前我正在同一个应用程序中测试它。插入方法工作正常。但是当我试图删除一行时。它最终删除了每一行,而不仅仅是我选择的那一行。我发送了要选择的子句和它应该删除的参数,所以我不明白为什么它删除每一行而不是只删除一行。

在 KontaktCP 中,似乎 delete 方法不认为我发送的是一个参数。因为它与 MKONTAKT 而不是 KONTAKT 匹配,因此删除每一行。

我目前已经对参数进行了硬编码,因为我只是在测试。但是我在运行程序时检查了数据库中是否存在值为1的_ID。并且其他 _ID 不是 1。

MainActivity中的相关代码:

public class MainActivity extends Activity
{
public static String PROVIDER="com.example.prosjekt3";
public static final Uri CONTENT_URI = Uri.parse("content://"+ PROVIDER + "/kontakt/");

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//Delete values //
String [] arguments = new String[1];
arguments[0] = "1";
String selectionclause = "_ID = ?";
getContentResolver().delete(CONTENT_URI, selectionclause, arguments);
}
// more code under here
}

KontaktCP中的相关代码

public class KontaktCP extends ContentProvider
{
private final static String TABLE_kontakt="Kontakter";
public final static String PROVIDER="com.example.prosjekt3";
public static final String KEY_ID="_ID";
private static final int KONTAKT =1;
private static final int MKONTAKT=2;

DBHandler DBhelper;
static SQLiteDatabase db;

public static final Uri CONTENT_URI = Uri.parse("content://"+ PROVIDER + "/kontakt");
private static final UriMatcher uriMatcher;
static
{
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
uriMatcher.addURI(PROVIDER, "kontakt",MKONTAKT);
uriMatcher.addURI(PROVIDER, "kontakt/#",KONTAKT);
}
@Override
public boolean onCreate()
{
DBhelper=new DBHandler(getContext());
db=DBhelper.getWritableDatabase();
return true;
}
public String getType(Uri uri)
{
switch (uriMatcher.match(uri))
{
case MKONTAKT:return "vnd.android.cursor.dir/vnd.example.kontakt";
case KONTAKT:return "vnd.android.cursor.item/vnd.example.kontakt";
default: return "Illegal uri"; // throw new IllegalArgumentException("Illeagal URI" + uri)
}
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) //matches MKontakt and deletes every row, instead of matching with KONTAKT
{
if (uriMatcher.match(uri)==KONTAKT)
{
db.delete(TABLE_kontakt, KEY_ID + "=" + uri.getPathSegments().get(1),selectionArgs);
getContext().getContentResolver().notifyChange(uri,null);
return 1;
}
if (uriMatcher.match(uri) == MKONTAKT)
{
db.delete(TABLE_kontakt,null,null);
getContext().getContentResolver().notifyChange(uri,null);
return 2;
}
return 0;
}
// more code under here

AndroidManifest.xml中的相关代码

<uses-permission android:name="android.permission.READ_CONTACTS" ></uses-permission>
<uses-permission android:name="com.example.prosjekt3.permission"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<permission android:name="com.example.prosjekt3.permission"/>

和:

    <provider android:name="com.example.prosjekt3.KontaktCP"
android:authorities="com.example.prosjekt3"
android:enabled="true"
android:exported="true"
android:readPermission="com.example.prosjekt3.permission"
android:writePermission="com.example.prosjekt3.permission">
</provider>

最佳答案

so i don't understand why it deletes every row instead of just one

发生这种情况是因为您将 CONTENT_URIdelete() 方法一起使用。现在,您在 delete() 方法中传递了一个对应于 MKONTAKTCONTENT_URI,这样您将删除所有内容。相反,CONTENT_URI 需要附加一个 id 以匹配 KONTAKT(如您在 UriMatcher 中声明的那样):

getContentResolver().delete(Uri.withAppendedPath(CONTENT_URI, "1"), selectionclause, arguments); 

关于android - "getContentResolver().delete()"删除每一行而不是只删除一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26488035/

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