在自定义内容提供程序中调用删除方法

发布于 2024-10-21 02:27:16 字数 1465 浏览 1 评论 0原文

我正在学习 Android,并且遇到了一个涉及调用自定义内容提供商的问题。我一直在使用一本指导书中的示例,尽管它描述了如何创建自定义提供程序,但没有明确的示例如何调用其中的特定方法。我正在专门研究如何从自定义内容提供商中删除单个记录。

以下是自定义内容提供程序 (EarthquakeProvider.java) 的代码:

@Override


public int delete(Uri uri, String where, String[] whereArgs) {
int count;

switch (uriMatcher.match(uri)) {
  case QUAKES:
    count = earthquakeDB.delete(EARTHQUAKE_TABLE, where, whereArgs);
    break;

  case QUAKE_ID:
    String segment = uri.getPathSegments().get(1);
    count = earthquakeDB.delete(EARTHQUAKE_TABLE, KEY_ID + "="
                                + segment
                                + (!TextUtils.isEmpty(where) ? " AND (" 
                                + where + ')' : ""), whereArgs);
    break;

  default: throw new IllegalArgumentException("Unsupported URI: " + uri);
}

getContext().getContentResolver().notifyChange(uri, null);
return count;


 }

我尝试从主活动中调用删除方法来删除单个条目,而不是整个数据库。我想对主活动的数组列表视图中显示的选定记录使用 OnLongClickListener

这是我迄今为止在该方法的主要活动中所想到的:

earthquakeListView.setOnItemLongClickListener(new OnItemLongClickListener() {

    @Override
    public boolean onItemLongClick(AdapterView _av, View _v, int _index,
            long arg3) {
        ContentResolver cr = getContentResolver();
        cr.delete(earthquakeProvider.CONTENT_URI, null, null); 

        return false;
    }

我知道上面的代码不起作用,但这已经是我目前的理解所能得到的最接近的了。

对此的任何帮助将非常感激。

I am learning Android and I am stuck on an issue involving calling a custom content provider. I have been using an example in an instructional book and although it describes how to create the custom provider there is no clear example how to call the specific methods in it. I am specifically looking into how to delete a single record from the custom content provider.

Here is the code for the custom content provider (EarthquakeProvider.java):

@Override


public int delete(Uri uri, String where, String[] whereArgs) {
int count;

switch (uriMatcher.match(uri)) {
  case QUAKES:
    count = earthquakeDB.delete(EARTHQUAKE_TABLE, where, whereArgs);
    break;

  case QUAKE_ID:
    String segment = uri.getPathSegments().get(1);
    count = earthquakeDB.delete(EARTHQUAKE_TABLE, KEY_ID + "="
                                + segment
                                + (!TextUtils.isEmpty(where) ? " AND (" 
                                + where + ')' : ""), whereArgs);
    break;

  default: throw new IllegalArgumentException("Unsupported URI: " + uri);
}

getContext().getContentResolver().notifyChange(uri, null);
return count;


 }

I am trying to call the delete method from the main activity to delete a single entry, not the entire database. I want to use about an OnLongClickListener for the selected record that is displayed in a array list view in the main activity.

This is what I have come up with I have so far in my main activity for this method:

earthquakeListView.setOnItemLongClickListener(new OnItemLongClickListener() {

    @Override
    public boolean onItemLongClick(AdapterView _av, View _v, int _index,
            long arg3) {
        ContentResolver cr = getContentResolver();
        cr.delete(earthquakeProvider.CONTENT_URI, null, null); 

        return false;
    }

I know the above code doesn't work, but this is as close as I could get with my current understanding.

Any help on this would be very much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

刘备忘录 2024-10-28 02:27:16
cr.delete(earthquakeProvider.CONTENT_URI, null, null);

这是你的问题。首先,一些上下文:

内容 URI:()

content://authority/path/##

末尾的数字是可选的。如果存在,URI 引用数据库中的特定行,其中 row._id=(数字)。如果不存在,它将引用整个表。

delete() 调用接受一个 URI、一个 where 子句和一组替换的字符串。示​​例:假设您有一个人员数据库。

cr.delete(
   Person.CONTENT_URI, 
   "sex=? AND eyecolor=?", 
   new String[]{"male", "blue"});

会搜索整个 person 表,并删除性别为男性且眼睛颜色为蓝色的任何人。

如果 where 子句和 where 值均为 null,则 delete() 调用将匹配表中的每一行。这会导致您看到的行为。

有两种方法可以指定您想要的行:

第一个选项,您可以将数字附加到 URI:

cr.delete(
    EarthquakeProvider.CONTENT_URI.buildUpon().appendPath(String.valueOf(_id)).build(),
    null, null);

这将 URI 限制为特定行,并且路径将通过您的 case QUAKE_ID: 语句因此无论如何都只会删除一行。

第二个选项,您可以使用 where 子句:

cr.delete(EarthquakeProvider.CONTENT_URI, "_id=?", String.valueOf(_id)));

无论哪种方式,您都可以根据需要将删除限制为单行。后者使代码更漂亮,但由于 ContentProvider 和 ContentObserver 的工作方式,前者更高效。

最后一点:在您的 ContentProvider 中,您需要添加一个调用
ContentResolver.notifyChange(Uri uri,ContentObserver 观察者,布尔值syncToNetwork)。这有助于通知游标重新获取数据库查询,并对自动化有很大帮助。

cr.delete(earthquakeProvider.CONTENT_URI, null, null);

This is your problem. First, some context:

Content URIs: (source)

content://authority/path/##

The number at the end is optional. If present, the URI references a specific row in the database where row._id=(the number). If absent, it references the table as a whole.

the delete() call accepts a URI, a where clause, and a set of strings which get substituted in. Example: Say you have a database of people.

cr.delete(
   Person.CONTENT_URI, 
   "sex=? AND eyecolor=?", 
   new String[]{"male", "blue"});

Will search the entire person table, and delete anyone whose sex is male and whose eye color is blue.

If the where clause and where values are null, then the delete() call will match every row in the table. This causes the behavior you see.

There are two methods to specify the row you want:

First option, you could append the number to the URI:

cr.delete(
    EarthquakeProvider.CONTENT_URI.buildUpon().appendPath(String.valueOf(_id)).build(),
    null, null);

This restricts the URI to a specific row, and the path will be through your case QUAKE_ID: statement and so will only delete one row no matter what.

Second option, you could use a where clause:

cr.delete(EarthquakeProvider.CONTENT_URI, "_id=?", String.valueOf(_id)));

Either way, you will restrict the delete to a single row, as you need it to. The latter makes for prettier code, but the former is more efficient, due to the way the ContentProvider and ContentObservers work.

As a last note: In your ContentProvider you need to add a call to
ContentResolver.notifyChange(Uri uri, ContentObserver observer, boolean syncToNetwork). This helps notify cursors to re-fetch the database query and helps out a lot with automation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文