选中列表视图中的复选框时如何删除文本

时间:2019-02-25 01:11:10

标签: java android

当我单击复选框时,我想删除文本。我尝试了以下方法,但无法正常工作。当我单击文本时,它会删除文本,并选中复选框,但不能以相反的方式工作。我仅将删除线状态保存在数据库中。如何从列表视图中获取复选框的位置,以便当我单击特定的复选框时,它仅删除该项目文本。请帮助我犯错的地方。

    protected class CurrentListClickListener implements OnItemClickListener {

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        int selectedItemNewStatus;
        final Item selectedItem = getItemAtPosition((ListView) parent, position);
        final CheckBox itemCheckbox = (CheckBox)view.findViewById(R.id.Item_checkbox);
        final TextView checkedTextView = (TextView) view.findViewById(R.id.Item_text);

        itemCheckbox.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (itemCheckbox.isChecked()){
                    checkedTextView.setPaintFlags(checkedTextView.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
                }
                else {
                    checkedTextView.setPaintFlags(checkedTextView.getPaintFlags() & ~Paint.STRIKE_THRU_TEXT_FLAG);
                }
            }
        });

        selectedItemNewStatus = selectedItem.getItemStatus() == ItemsTable.ITEM_STATUS_SELECTED ? ItemsTable.ITEM_STATUS_NOT_SELECTED
                : ItemsTable.ITEM_STATUS_SELECTED;

        selectedItem.setItemStatus(selectedItemNewStatus);

        getContentResolver().update(
                ContentUris.withAppendedId(MainProvider.URI_CONTENT_ITEMS, selectedItem.getId()),
                selectedItem.getContentValues(), null, null);

    }
}

这是我的ItemsCursorAdapter

public ItemsCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int flags) {
        super(context, layout, c, from, to, flags);
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {

        // Get data of the selected item
        String itemName = cursor.getString(cursor.getColumnIndex(ItemsTable.C_NAME));
        int itemStatus = cursor.getInt(cursor.getColumnIndex(ItemsTable.C_STATUS));

        // Set the textView with the item name and its status
        TextView checkedTextView = (TextView) view.findViewById(R.id.Item_text);
        CheckBox itemCheckbox = (CheckBox)view.findViewById(R.id.Item_checkbox);
        checkedTextView.setText(itemName);



        switch (itemStatus) {
        case ItemsTable.ITEM_STATUS_SELECTED:
            checkedTextView.setPaintFlags(checkedTextView.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
            itemCheckbox.setChecked(true);
            break;
        case ItemsTable.ITEM_STATUS_NOT_SELECTED:
            checkedTextView.setPaintFlags(checkedTextView.getPaintFlags() & ~Paint.STRIKE_THRU_TEXT_FLAG);
            itemCheckbox.setChecked(false);
            break;
        default:
            break;
        }

    }

}

0 个答案:

没有答案