自定义arrayadapter无法删除项

时间:2016-01-31 20:28:10

标签: android

我正在建一家书店。当我想退房时,我在购物车中选择一些书籍,然后点击退房。签出后,签出的项目应从列表视图中删除。

我使用startActivityForResult发送我选择的数据,在签出后,onActivityResult管理删除选中的项目。这是部分代码:

public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
            switch (item.getItemId()) {
                case R.id.delete:
                    // Calls getSelectedIds method from ListViewAdapter Class
                    SparseBooleanArray del_selected = viewHolderAdapter
                            .getSelectedIds();
                    // Captures all selected ids with a loop
                    for (int i = (del_selected.size() - 1); i >= 0; i--) {
                        if (del_selected.valueAt(i)) {
                            Book del_selectedItem = viewHolderAdapter
                                    .getItem(del_selected.keyAt(i));
                            // Remove selected items following the ids
                            viewHolderAdapter.remove(del_selectedItem);
                        }
                    }
                    // Close CAB
                    mode.finish();
                    return true;
                case R.id.checkout:
                    Intent intent_checkout = new Intent(BookStoreActivity.this, CheckoutActivity.class);
                    SparseBooleanArray chk_selected = viewHolderAdapter.getSelectedIds();
                    ArrayList<Book> checkout_Cart = new ArrayList<Book>();
                    for(int i = (chk_selected.size() - 1); i >= 0; i--) {
                        if (chk_selected.valueAt(i)) {
                            Book chk_selectedItem = viewHolderAdapter.getItem(chk_selected.keyAt(i));
                            checkout_Cart.add(chk_selectedItem);
                        }
                    }
                    intent_checkout.putExtra("Cart", checkout_Cart);
                    startActivityForResult(intent_checkout, CHECKOUT_REQUEST);
                    mode.finish();
                    return true;
                default:
                    return false;
            }
        }

protected void onActivityResult(int requestCode, int resultCode,
        Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);
    if(requestCode == ADD_REQUEST) {
        if (resultCode == RESULT_OK) {
            Bundle data = intent.getExtras();
            Book book = (Book) data.getParcelable("book");
            shoppingCart.add(book);

            viewHolderAdapter.notifyDataSetChanged();
        }
    }
    if(requestCode == CHECKOUT_REQUEST) {
        if(resultCode == RESULT_OK) {
            Bundle data = intent.getExtras();
            ArrayList<Book> chk_selectedItems = new ArrayList<Book>();
            chk_selectedItems = data.getParcelableArrayList("Cart");
            for(int i = 0; i < chk_selectedItems.size(); i++) {
                Book chk_selectedItem = chk_selectedItems.get(i);
                Log.d("**********", chk_selectedItem.getTitle());
                viewHolderAdapter.remove(chk_selectedItem);
            }
        }
    }

}

在ViewHolderAdapter中:

public void remove(Book object) {
        shoppingCart.remove(object);
        notifyDataSetChanged();
    }

当我可以直接从列表中删除项目时,它运行良好。但是当我退房时,所选项目仍在那里。 logCat显示 chk_selectedItem.getTitle()的结果。 提前谢谢。

0 个答案:

没有答案