2

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>
deucalion0
  • 2,422
  • 9
  • 55
  • 99

1 Answers1

1

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?

Community
  • 1
  • 1
Carlos
  • 5,991
  • 6
  • 43
  • 82
  • Thank you for your response! I can understand kind of how tags work looking at the link you provided, but I'm unsure of how I can implement this to the button in my XML. Could you provide further insight!? Thank you!! – deucalion0 Jul 27 '14 at 19:54
  • I'm on my pad, so I can't write much. Somewhere in your code you will be assigning properties to each subview. So text view.settext ("text for item 2") etc. At that stage find the delete button and set the tag. – Carlos Jul 27 '14 at 20:44
  • Carlos, I am not sure about assigning properties, all I do is pass a CusrorAdapter a String array populated from my database, I am not sure how I can tag the Buttons at this point? Thanks! – deucalion0 Jul 28 '14 at 14:33
  • Looking at a project I have, there's a bindView(View view, Context context, Cursor cursor) method. Inside of that, you see things like "TextView tcDesc = (TextView) view.findViewById(R.id.db_item_title);String desc = cursor.getString(cursor.getColumnIndex(DBHelper.COL_SHORT_DESC));tcDesc.setText(desc);" That is where each individual view is populated. – Carlos Jul 28 '14 at 14:44