【问题标题】:Have ListView selected checkbox delete from database让 ListView 选中的复选框从数据库中删除
【发布时间】:2012-07-20 13:14:40
【问题描述】:

我正在为我的 ListView 使用自定义列表适配器。如何从选中的复选框中获取数据库 rowid/_id 以便我可以从数据库中多次删除数据?

这是我的活动:

import android.app.ListActivity;
import java.text.DecimalFormat;
import org.yhw.PlanWithMe.R;
import android.content.*;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.database.Cursor;

public class Finance_delete extends ListActivity {
    Cursor cursor;
    private TextView dateText;
    private Button btnCancel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.finance_delete);

        registerForContextMenu(this.getListView());
        DBAdapter dbHelper = new DBAdapter(this);
        dbHelper.open();

        // Get a Cursor for the list items
        Intent intent = getIntent();
        String date = intent.getStringExtra("date");
        Cursor listCursor = dbHelper.getAllFinance(date);
        startManagingCursor(listCursor);

        // set the custom list adapter
        setListAdapter(new MyListAdapter(this, listCursor));

        dateText = (TextView) findViewById(R.id.lblDate);
        dateText.setText("Delete Spending: " + date);

        btnCancel = (Button) findViewById(R.id.btnCancel);
        btnCancel.setOnClickListener(click_listener);
    }

    private class MyListAdapter extends ResourceCursorAdapter {

        public MyListAdapter(Context context, Cursor cursor) {
            super(context, R.layout.list_item_with_description_delete, cursor);
        }

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

            CheckBox cbListCheck = (CheckBox) view
                    .findViewById(R.id.list_checkbox);
            cbListCheck.setChecked(false);

            cbListCheck
                    .setOnCheckedChangeListener(new OnCheckedChangeListener() {
                        public void onCheckedChanged(CompoundButton cb,
                                boolean isChecked) {
                            if (cb.isChecked()) {
                                // action


                            } else if (isChecked == false) {
                                // action

                            }
                        }
                    });

            TextView title = (TextView) view.findViewById(R.id.list_category);
            title.setText(cursor.getString(cursor
                    .getColumnIndex(DBAdapter.KEY_Category)));

            TextView details = (TextView) view.findViewById(R.id.list_desc);
            StringBuffer detailsText = new StringBuffer();

            String description = cursor.getString(cursor
                    .getColumnIndex(DBAdapter.KEY_Desc));

            TextView spendings = (TextView) view
                    .findViewById(R.id.list_spending);

            String spending = cursor.getString(cursor
                    .getColumnIndex(DBAdapter.KEY_Spending));

            double tgtspend = Double.parseDouble(spending);
            DecimalFormat dFormat = new DecimalFormat("0.00");
            String formatSpend = dFormat.format(tgtspend);
            spendings.setText("$" + formatSpend);

            if (description != null && description.length() > 0) {
                detailsText.append(description);
            } else {
                detailsText.append("-");
            }
            details.setText(detailsText.toString());

        }
    }

    private OnClickListener click_listener = new OnClickListener() {
        public void onClick(View view) {
            // Intent intent = null;
            switch (view.getId()) {
            case R.id.btnCancel:
                finish();
                break;
            }
        }
    };
}

我的活动布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/finance_top" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="45dp"
        android:src="@drawable/navbar" />

    <TextView
        android:id="@+id/lblDate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="57dp"
        android:layout_alignParentLeft="true"
        android:textColor="#ffffff"
        android:textSize="16dp" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="240dp"
        android:layout_marginTop="48dp"
        android:src="@drawable/divider" />

    <Button
        android:id="@+id/btnTick"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_marginRight="45dp"
        android:layout_marginTop="56dp"
        android:background="@drawable/btn_tick" />

    <Button
        android:id="@+id/btnCancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_marginRight="6dp"
        android:layout_marginTop="56dp"
        android:background="@drawable/btn_cancel" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="282dp"
        android:layout_marginTop="48dp"
        android:src="@drawable/divider" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginTop="92dp"
        android:orientation="vertical" >

        <ListView
            android:id="@android:id/list"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1" />

        <TextView
            android:id="@android:id/empty"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="106dp"
            android:layout_marginTop="180dp"
            android:text="@string/finance_note"
            android:textColor="#ffffff"
            android:textSize="18dp" />
    </LinearLayout>

</RelativeLayout>

我的 ListView 布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="?android:attr/listPreferredItemHeight" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_weight="1"
        android:duplicateParentState="true" >

        <TextView
            android:id="@+id/list_category"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:gravity="center_vertical"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <TextView
            android:id="@+id/list_desc"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@id/list_category"
            android:singleLine="true"
            android:textAppearance="?android:attr/textAppearanceSmall" />

        <TextView
            android:id="@+id/list_spending"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:gravity="center"
            android:paddingBottom="10dip"
            android:paddingTop="10dip"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <CheckBox
            android:id="@+id/list_checkbox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="false"
            android:text="" >
        </CheckBox>
    </RelativeLayout>

</LinearLayout>

【问题讨论】:

    标签: android sqlite listview checkbox


    【解决方案1】:

    1) 使用 checkbox.setTag 属性将您的复选框与 ID 关联起来。

    2) 在您的删除按钮单击事件中执行以下逻辑。

    • 遍历所有复选框
    • 使用 checkbox.getTag 属性收集被检查者的 ID,并以逗号分隔。
    • 对删除操作使用逗号分隔值。

    希望您明白这一点,请检查此link for conceptual 实现。

    【讨论】:

    • 我试过这个方法。当我检查时,我将获取 rowid 并将其仍然保存为字符串。多重检查将 +="," 到字符串。但是如果我取消选中它,id 仍然在字符串中。无论如何要从该字符串中删除 id 吗?
    • 请再次阅读我的回答;您正在尝试在每次单击 CHECKBOX 时对其进行管理,而我建议您在按下删除按钮后通过循环遍历所有元素来收集 ID,并选择那些已选择的 ID 等于选中...这样您将拥有所有检查其值的 ID。
    • 我可以提供任何编码示例吗?我不太确定该怎么做
    • 你先明白这个概念了吗?
    • 是的,但我不知道如何开始编码。我是初学者
    猜你喜欢
    • 2023-03-07
    • 2017-11-26
    • 2015-11-13
    • 2015-03-18
    • 1970-01-01
    • 2013-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多