Note that there are some explanatory texts on larger screens.

plurals
  1. POHave ListView selected checkbox delete from database
    primarykey
    data
    text
    <p>I'm using a custom list adapter for my ListView. How can I get the database rowid/_id from the checked checkbox so that I can multiple delete the data from my database?</p> <p>Here's my activity:</p> <pre><code>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 &amp;&amp; description.length() &gt; 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; } } }; } </code></pre> <p>My Activity layout:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" &gt; &lt;ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/finance_top" /&gt; &lt;ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="45dp" android:src="@drawable/navbar" /&gt; &lt;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" /&gt; &lt;ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="240dp" android:layout_marginTop="48dp" android:src="@drawable/divider" /&gt; &lt;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" /&gt; &lt;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" /&gt; &lt;ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="282dp" android:layout_marginTop="48dp" android:src="@drawable/divider" /&gt; &lt;LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_marginTop="92dp" android:orientation="vertical" &gt; &lt;ListView android:id="@android:id/list" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" /&gt; &lt;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" /&gt; &lt;/LinearLayout&gt; &lt;/RelativeLayout&gt; </code></pre> <p>My ListView layout:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="?android:attr/listPreferredItemHeight" &gt; &lt;RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_weight="1" android:duplicateParentState="true" &gt; &lt;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" /&gt; &lt;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" /&gt; &lt;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" /&gt; &lt;CheckBox android:id="@+id/list_checkbox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="false" android:text="" &gt; &lt;/CheckBox&gt; &lt;/RelativeLayout&gt; &lt;/LinearLayout&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload