deucalion0
deucalion0

Reputation: 2440

Delete items in List View populated by SQLite Database using a delete button in each list item in Android

I have spent two days trying to figure this out using many answers on Stack Overflow, I am more confused than ever about how to achieve this.

I populate a list from data in my SQLite database. I am trying to figure out how to delete a row from the database by clicking a button that is inside the same list item next to the text view which holds the database row string.

I cannot figure out how to give each button an ID that is tied to each list item, if I could do this I could get the string from the list item then pass it to a query to delete a row with that string from the SQLite database. Any insight is appreciated.

Here is a simplified version of my code for populating the list:

public class ShowPhrases extends Activity implements OnInitListener {

ListView lv;
Button trash;
private static final String fields[] = { "phrase", "folang", "engornot",
            BaseColumns._ID };

    NotesDbAdapter.DatabaseHelper helper = new NotesDbAdapter.DatabaseHelper(
            this);

    @Override
    public void onCreate(Bundle savedInstanceState) {
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.hs);

        data.moveToNext();

        dataSource = new SimpleCursorAdapter(this, R.layout.phrasebook, data,
                fields, new int[] { R.id.first }, 0);

        lv = (ListView) findViewById(R.id.list_view);

        lv.setAdapter(dataSource);

}


}

Here is my Layout that includes my ListView:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ffffff"
    android:gravity="center"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/languageHolder"
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:clickable="true"
        android:orientation="horizontal" >

        <LinearLayout
            android:id="@+id/british"
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:layout_weight="0.5"
            android:background="@drawable/briton"
            android:clickable="true"
            android:orientation="vertical" >
        </LinearLayout>

         <LinearLayout
            android:id="@+id/italian"
              android:layout_width="0dp"
            android:layout_height="50dp"
            android:layout_weight="0.5"
            android:background="@drawable/itaoff"
            android:clickable="true"
            android:orientation="vertical" >
        </LinearLayout>
    </LinearLayout>

    <ListView
        android:id="@+id/list_view"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:divider="#cccccc"
        android:dividerHeight="4px" />


</LinearLayout>

And the ListView XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rowLayout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#ffffff"
    android:orientation="horizontal"
    android:paddingLeft="10dp"
    android:paddingRight="10dp" >

    <TextView
        android:id="@+id/first"
        style="@style/factsText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginTop="20dp"
        android:focusable="false" />

    <LinearLayout
        android:layout_width="30dp"
        android:layout_height="fill_parent"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:orientation="vertical" >

        <Button
            android:id="@+id/trashButton"
            android:layout_width="200dp"
            android:layout_height="40dp"
            android:gravity="center_vertical"
            android:text="B" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/spacer"
        android:layout_width="20dp"
        android:layout_height="fill_parent"
        android:layout_centerVertical="true"
        android:layout_toLeftOf="@+id/trash"
        android:orientation="vertical" >
    </LinearLayout>

    <LinearLayout
        android:id="@+id/favourite"
        android:layout_width="30dp"
        android:layout_height="fill_parent"
        android:layout_centerVertical="true"
        android:layout_toLeftOf="@+id/spacer"
        android:orientation="vertical" >

        <ImageView
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:gravity="center_vertical"
            android:src="@drawable/starfav" />
    </LinearLayout>

</RelativeLayout>

Upvotes: 2

Views: 657

Answers (1)

Carlos
Carlos

Reputation: 6021

At some stage, your adapter is called to populate each item in the list. You could use the tag of each entry (or just the tag of the delete button) to record the corresponding ID in the database.

Here's more:

What is the main purpose of setTag() getTag() methods of View?

Upvotes: 1

Related Questions