为了账号安全,请及时绑定邮箱和手机立即绑定

如何使用光标获取项目的位置并删除它?

如何使用光标获取项目的位置并删除它?

繁花不似锦 2024-01-28 16:45:57
我正在制作包含书签的webview应用程序,我使用Cursor和SQLite 数据库成功将项目添加到listView中,但我的问题是我无法获取项目的位置并删除它我尝试以自己的方式执行此操作,但我得到的是我已经删除了列表视图的所有内容。这是我的MainActivity,我制作了一个longClickItemListener来删除长按时的列表视图项目,这是我的BookmarkDatabase主要活动final Dialog bookmarksScreen = new Dialog(this);        bookmarksScreen.requestWindowFeature(Window.FEATURE_NO_TITLE);        bookmarksScreen.setContentView(R.layout.activity_bookmark);        bookmarksScreen.setCancelable(true);        final ListView listView = bookmarksScreen.findViewById(R.id.bookmark_list);        ImageView bookmarkBack   = bookmarksScreen.findViewById(R.id.close_bookmarks);        ImageView bookmarkDelete = bookmarksScreen.findViewById(R.id.delete_table);        // populate an ArrayList<String> from the database and then view it        final ArrayList<String> theList = new ArrayList<>();        Cursor data = bookmarkDB.getListContents();        // check if there is no data on database        if(data.getCount() == 0){            Log.d("Database Bookmark", "There is no items on item list");        } else {            while(data.moveToNext()) {                // add data into table "COL1" and "COL2"                theList.add(data.getString(1));                ListAdapter listAdapter = new ArrayAdapter<>(this, R.layout.activity_listitem, theList);                listView.setAdapter(listAdapter);            }        }        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {            @Override            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {                String item = parent.getItemAtPosition(position).toString();                webView.loadUrl(item);                bookmarksScreen.dismiss();            }        });
查看完整描述

1 回答

?
ITMISS

TA贡献1871条经验 获得超8个赞

理想情况下,您将在视图中可检索的地方使用主键(或者反之亦然,使用已在可检索的地方使用的值作为主键)。由于这里的主键是一个auto_increment字段,因此它与您尝试删除的视图之间没有真正可靠的关联(至少从我在您的代码中看到的)。一种方法是Adapter为您的列表使用自定义项,而不是ArrayAdapter, 并使用View.setTag()View.getTag()方法来存储列表中每个项目的主键。

假设您的activity_listitem布局有一个TextViewwith id text_view,您在其中显示从数据库获取的文本,并且您已添加存储主键的标签,您可以在主活动中执行以下操作:

    listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

        @Override

        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

            TextView textView = (TextView) view.findViewById(R.id.text_view);

            final Integer which_item = (Integer) textView.getTag();    //Assuming you stored the ID as an Integer

            int rowsDeleted = bookmarksDB.deleteSpecificContent(which_item);

            if(rowsDeleted == 0){  //should be 1, since we're deleting by Primary Key

                //if you would like like, handle if nothing was deleted

            }

            return true;

        }

    });

在您的 BookmarksDatabase 类中:


    public int deleteSpecificContents(int id) {

        SQLiteDatabase db = this.getWritableDatabase();

        return db.delete(TABLE_NAME, COL1 + "=?", new String[]{Integer.toString(id)});

    }


如果您不想使用自定义Adapter,另一种方法是使用列表中的位置来确定要删除的项目。您可以通过查询数据库并迭代Cursor直到它与列表中项目的位置匹配来完成此操作。


    listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

        @Override

        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

            Cursor data = bookmarkDB.getListContents();

            int i = 0;

            while(data.moveToNext()){

                if(i == position){

                    break;

                }

                i++;

            }

            int rowsDeleted = bookmarkDB.deleteSpecificContent(data.getInt(0));

            //again, handle if rowsDeleted != 1 if you want

            return true;

        }

    });

您的BookmarkDatabase.deleteSpecificContent()方法与第一种方法完全相同 - 唯一的区别在于如何确定要删除的项目的 id。如果您不想处理deleteSpecificContent()不返回 1 的情况(除非我在这里遗漏了一些主要内容,否则在您的用例中它应该始终返回 1),请随意将其签名更改为void, 而不是int。


请注意,第二种方法比第一种方法更昂贵,因为它需要额外的数据库查询,并且可能会迭代很多Cursor。虽然它需要更少的新/更改的代码,但可能不建议使用(老实说我不能肯定地说。这可能适合您的用例,具体取决于您的数据库的大小,但是 - 完全披露 - 我是没有足够的 Android 经验来拨打此电话。请自行决定使用)。


查看完整回答
反对 回复 2024-01-28
  • 1 回答
  • 0 关注
  • 30 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信