繁体   English   中英

如何使用特定于项目的删除按钮删除列表视图中的项目?

[英]How to delete an item in my list view using the items specific delete button?

我有一个 listView,每个项目都有一个删除按钮。 我希望能够在单击项目按钮时删除该项目。

我曾尝试使用 .remove (position) 和 [position] = null 但它不起作用。 我不确定是因为我使用了数据库和 cursorAdapter 还是因为我是 Android Studio 的新手,我只是不知道在 .remove 之前应该使用什么实际变量。 据我所知,我的代码很乱,但到目前为止还可以。

private void populatelistView() {
            final Cursor res = userDb.getAllRows();
            final String[] fromFeildnames = new String[]{ DatabaseUser.KEY_1, DatabaseUser.KEY_2};
            final int[] toViewIds = new int[]{R.id.textViewNum, R.id.textViewItem};
            final SimpleCursorAdapter myCursorAdaptor;
            myCursorAdaptor = new SimpleCursorAdapter(getBaseContext(), R.layout.item_layout, res, fromFeildnames, toViewIds, 0){
                @Override
                public View getView(int position, View convertView, ViewGroup parent) {
                    LayoutInflater inflater = getLayoutInflater();
                    View rowView = inflater.inflate(R.layout.item_layout, null, true);
                    delete= (Button)rowView.findViewById(R.id.deleteItem);
                    delete.setTag(position);


                    Cursor cursor = (Cursor) mylist.getItemAtPosition(position);
                    String listName = cursor.getString(1);
                    String displayName = db.getName(listName);

                    TextView textView = (TextView) rowView.findViewById(R.id.textViewItem);
                    textView.setText(displayName);
View.OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            View parentRow = (View) v.getParent();
                            ListView listView = (ListView) parentRow.getParent();
                            final int position = listView.getPositionForView(parentRow);
                            Cursor cursor = (Cursor) mylist.getItemAtPosition(position);
                            Toast.makeText(getApplicationContext(), "Button " + position, Toast.LENGTH_SHORT).show();
                        }
                    });
                    return rowView;
                }
            };
            mylist.setAdapter(myCursorAdaptor);
        }

你有没有尝试做这样的事情:

myList.remove([INDEX]);
myList.notifyDataSetChanged(); or adapter.notifyDataSetChanged();

它应该工作! 如果没有告诉我!

更多信息: 如何使用按钮从列表视图中删除项目?

永不放弃

我不明白为什么在使用SimpleCursorAdapter的目的时将cursor放在mylist ,您可以通过调用getCursor()方法来获取cursor实例。

要回答您的问题,请按照以下步骤操作:

UserDb类中创建删除方法

class UserDb {

    void delete(String userId) {
        mSqLiteDatabase.delete(USER_TABLE,
                "id = ?",
                new String[]{userId});
    }
}

onClickListener设置为您的删除按钮

delete.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        int deletePosition = (int) v.getTag();
        cursor.moveToPosition(deletePosition);
        String userId = cursor.getString(1); // I assume this is the user id
        userDb.delete(userId);

        // get the cursor that contains the newest data
        Cursor newestDataCursor = userDb.getAllRows();

        // change your adapter cursor
        myCursorAdaptor.changeCursor(newestDataCursor);

        // tell your adapter that the data has been changed, so it needs to update it's views
        myCursorAdaptor.notifyDataSetChanged();
    }
});

仅供您考虑,您可以使用

  • RecyclerView以获得更好的性能和内存处理。
  • ORM (对象关系映射)库,如Room甚至Firebase ,让您的生活更轻松。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM