deleting rows for a button click in android sqlite
<p>I am new to android sqlite, what i want to do exactly mean i have <code>main.xml</code> page with Five fields like sculpture, arcthitecural integration , mural, landscape, historical site with ON and OF buttons,for exampleif the sculpture s in OFF mode initially if the user clicks on ON btn, the off button status like "Sculpture", "OFF" is should be deleted, and the ON btn status "Sculpture" "ON" value should be inserted, in my code i have deleted the rows corresponding button clicks, the problem s for deleting a row i need to assign the value to row id, but each and every button click row id ll be increment, so Pls help me how to delete ON btn status without assigning the row id value, i will paste my whole code here,</p>

<p>main.xml:</p>

<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/layout1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" android:background="#FFFFFF"&gt;
 &lt;TableRow android:id="@+id/tableRow1" android:layout_marginTop="35dp" android:layout_width="wrap_content" android:layout_height="wrap_content"&gt;
  &lt;ImageView android:layout_marginTop="-5dp" android:src="@drawable/sculp" android:id="@+id/sclpimg" android:layout_height="wrap_content" android:layout_width="wrap_content" /&gt; 
  &lt;TextView android:text="Sculpture" android:textSize="20dp" android:textStyle="bold" android:layout_marginLeft="15dp" android:id="@+id/sclptxt" android:layout_width="wrap_content" android:layout_height="wrap_content" /&gt; 
  &lt;Button android:text="ON" android:id="@+id/sclpbtnOn" android:layout_width="50dp" android:layout_height="35dp" /&gt; 
  &lt;Button android:text="OFF" android:id="@+id/sclpbtnOff" android:layout_width="50dp" android:layout_height="35dp" /&gt; 
  &lt;/TableRow&gt;
  &lt;View android:id="@+id/view1" android:background="#000000" android:layout_width="fill_parent" android:layout_height="2dp" /&gt; 
&lt;TableRow android:id="@+id/tableRow2" android:layout_marginTop="16dp" android:layout_width="wrap_content" android:layout_height="wrap_content"&gt;
  &lt;ImageView android:src="@drawable/arch" android:id="@+id/arcimg" android:layout_height="wrap_content" android:layout_width="wrap_content" /&gt; 
  &lt;TextView android:text="" android:textSize="20dp" android:textStyle="bold" android:layout_marginTop="10dp" android:layout_marginLeft="10dp" android:id="@+id/arctxt" android:layout_width="wrap_content" android:layout_height="wrap_content" /&gt; 
  &lt;Button android:text="ON" android:id="@+id/arcbtnOn" android:layout_marginTop="10dp" android:layout_width="50dp" android:layout_height="35dp" /&gt; 
  &lt;Button android:text="OFF" android:id="@+id/arcbtnOff" android:layout_marginTop="10dp" android:layout_width="50dp" android:layout_height="35dp" /&gt; 
  &lt;/TableRow&gt;
  &lt;View android:id="@+id/view1" android:background="#000000" android:layout_width="fill_parent" android:layout_height="2dp" /&gt; 
 &lt;TableRow android:id="@+id/tableRow3" android:layout_marginTop="13dp" android:layout_width="wrap_content" android:layout_height="wrap_content"&gt;
  &lt;ImageView android:src="@drawable/land" android:id="@+id/landimg" android:layout_height="wrap_content" android:layout_width="wrap_content" /&gt; 
  &lt;TextView android:text="Landscape" android:layout_marginLeft="10dp" android:textSize="20dp" android:textStyle="bold" android:id="@+id/landtxt" android:layout_width="wrap_content" android:layout_height="wrap_content" /&gt; 
  &lt;Button android:text="ON" android:id="@+id/landbtnOn" android:layout_width="50dp" android:layout_height="35dp" /&gt; 
  &lt;Button android:text="OFF" android:id="@+id/landbtnOff" android:layout_width="50dp" android:layout_height="35dp" /&gt; 
  &lt;/TableRow&gt;
  &lt;View android:id="@+id/view1" android:layout_marginTop="8dp" android:background="#000000" android:layout_width="fill_parent" android:layout_height="2dp" /&gt; 
 &lt;TableRow android:layout_width="wrap_content" android:layout_marginTop="18dp" android:id="@+id/tableRow4" android:layout_height="wrap_content"&gt;
  &lt;ImageView android:src="@drawable/mural" android:layout_marginTop="-5dp" android:id="@+id/muralimg" android:layout_height="wrap_content" android:layout_width="wrap_content" /&gt; 
  &lt;TextView android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:textSize="20dp" android:textStyle="bold" android:layout_marginTop="10dp" android:layout_width="wrap_content" android:id="@+id/muraltxt" android:text="Mural" /&gt; 
  &lt;Button android:layout_height="35dp" android:layout_marginTop="10dp" android:layout_width="50dp" android:id="@+id/muralbtnOn" android:text="ON" /&gt; 
  &lt;Button android:layout_height="35dp" android:layout_marginTop="10dp" android:layout_width="50dp" android:id="@+id/muralbtnOff" android:text="OFF" /&gt; 
  &lt;/TableRow&gt;
  &lt;View android:id="@+id/view1" android:background="#000000" android:layout_width="fill_parent" android:layout_height="2dp" /&gt; 
 &lt;TableRow android:layout_width="wrap_content" android:layout_marginTop="16dp" android:id="@+id/tableRow5" android:layout_height="wrap_content"&gt;
  &lt;ImageView android:src="@drawable/historic" android:id="@+id/histimg" android:layout_height="wrap_content" android:layout_width="wrap_content" /&gt; 
  &lt;TextView android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:textSize="20dp" android:textStyle="bold" android:layout_marginTop="10dp" android:layout_width="wrap_content" android:id="@+id/histimg" android:text="Historic Sites" /&gt; 
  &lt;Button android:layout_height="35dp" android:layout_marginTop="10dp" android:layout_width="50dp" android:id="@+id/histbtnOn" android:text="ON" /&gt; 
  &lt;Button android:layout_height="35dp" android:layout_marginTop="10dp" android:layout_width="50dp" android:id="@+id/histbtnOff" android:text="OFF" /&gt; 
  &lt;/TableRow&gt;
 &lt;ListView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/listView1"&gt;&lt;/ListView&gt;
  &lt;/TableLayout&gt;
</code></pre>

<p>row.xml:</p>

<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content"&gt;
&lt;LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content"&gt;
  &lt;TextView android:layout_width="wrap_content" android:layout_height="fill_parent" android:padding="2dip" android:text="#" /&gt; 
  &lt;TextView android:id="@+id/id" android:layout_width="wrap_content" android:layout_height="fill_parent" android:padding="2dip" android:paddingRight="10dip" /&gt; 
  &lt;TextView android:layout_width="wrap_content" android:layout_height="fill_parent" android:padding="2dip" android:paddingRight="10dip" android:text="-" /&gt; 
  &lt;TextView android:id="@+id/text1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="2dip" /&gt; 
  &lt;/LinearLayout&gt;
  &lt;TextView android:id="@+id/text2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="2dip" /&gt; 
  &lt;/LinearLayout&gt;
</code></pre>

<p>Main activity class:</p>

<pre><code>package com.paad.databaseC;




import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.CompletionInfo;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class databaseC extends Activity {
    Button sculpon,arcon,muralon,histon,landon;
    Button sculpoff,arcoff,muraloff,histoff,landoff;

    private SQLiteAdapter mySQLiteAdapter;
    ListView listContent;

    SimpleCursorAdapter cursorAdapter;
    Cursor cursor;
    protected Object KEY_ID;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        sculpon=(Button)findViewById(R.id.sclpbtnOn);
        arcon = (Button)findViewById(R.id.sclpbtnOff);
        muralon = (Button)findViewById(R.id.muralbtnOn);
        histon = (Button)findViewById(R.id.histbtnOn);
        landon = (Button) findViewById(R.id.landbtnOn);
        //off button mapping
        sculpoff = (Button)findViewById(R.id.sclpbtnOff);
        arcoff = (Button)findViewById(R.id.arcbtnOff);
        muraloff = (Button)findViewById(R.id.muralbtnOff);
        histoff = (Button)findViewById(R.id.histbtnOff);
        landoff = (Button)findViewById(R.id.landbtnOff);

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

        mySQLiteAdapter = new SQLiteAdapter(this);
        mySQLiteAdapter.openToWrite();

        cursor = mySQLiteAdapter.queueAll();
        String[] from = new String[]{SQLiteAdapter.KEY_ID, SQLiteAdapter.KEY_CONTENT1, SQLiteAdapter.KEY_CONTENT2};
        int[] to = new int[]{R.id.id, R.id.text1, R.id.text2};
        cursorAdapter =
            new SimpleCursorAdapter(this, R.layout.row, cursor, from, to);
        lv.setAdapter(cursorAdapter);
        lv.setOnItemClickListener(lvOnItemClickListener);
        // code for on buttons
        sculpon.setOnClickListener(sculponOnClickListener);
        arcon.setOnClickListener(arconOnClickListener);
        histon.setOnClickListener(histonOnClickListener);
       landon.setOnClickListener(landonOnClickListener);
       muralon.setOnClickListener(muralonOnClickListener);

       // code for off buttons

       sculpoff.setOnClickListener(sculpoffOnClickListener);
      arcoff.setOnClickListener(arcoffOnClickListener);
       histoff.setOnClickListener(histoffOnClickListener);
       landoff.setOnClickListener(landoffOnClickListener);
       muraloff.setOnClickListener(muraloffOnClickListener);
    }

    OnClickListener sculponOnClickListener
    = new Button.OnClickListener(){

        @Override
        public void onClick(View arg0) {

int id=34;
mySQLiteAdapter.delete_byID(id);

                        String v1="Sculpture";
            String v2 = "ON";
            mySQLiteAdapter.insert(v1, v2);
            //mySQLiteAdapter.updateAll(v1, v2);
            updateList();
        }
};


OnClickListener arconOnClickListener
= new Button.OnClickListener(){

    @Override
    public void onClick(View arg0) {

    int id=24;
mySQLiteAdapter.delete_byID(id);

    String v1="Architectural Integration";
    String v2 = "ON";
        mySQLiteAdapter.insert(v1, v2);
        updateList();
    }
};

OnClickListener landonOnClickListener
= new Button.OnClickListener(){

    @Override
    public void onClick(View arg0) {

    int id=21;
        mySQLiteAdapter.delete_byID(id);
        String v1="Landscape";
        String v2 = "ON";
        mySQLiteAdapter.insert(v1, v2);
        updateList();
    }
};

OnClickListener muralonOnClickListener
= new Button.OnClickListener(){

    @Override
    public void onClick(View arg0) {

int id=4;
        mySQLiteAdapter.delete_byID(id);

        String v1="Mural";
        String v2 = "ON";
        mySQLiteAdapter.insert(v1, v2);
        updateList();
    }
};


OnClickListener histonOnClickListener
= new Button.OnClickListener(){

    @Override
    public void onClick(View arg0) {

        int id=16;
        mySQLiteAdapter.delete_byID(id);

                    String v1="Historical Site";
        String v2 = "ON";
        mySQLiteAdapter.insert(v1, v2);
        updateList();
    }
};





private ListView.OnItemClickListener lvOnItemClickListener
= new ListView.OnItemClickListener(){

    @Override
    public void onItemClick(AdapterView&lt;?&gt; arg0, View arg1, int arg2, long arg3) {
        // TODO Auto-generated method stub

    }


     }; 
// click events for off buttons
   OnClickListener sculpoffOnClickListener = new Button.OnClickListener(){

    @Override
    public void onClick(View v) {
int id=1;
        mySQLiteAdapter.delete_byID(id);

        String v1="Sculpture ";
        String v2 = "OFF";
            mySQLiteAdapter.insert(v1, v2);
                updateList();
    }

   };

   OnClickListener arcoffOnClickListener = new Button.OnClickListener(){

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

    int id=13;
        mySQLiteAdapter.delete_byID(id);

            String v1="Architectural Integration ";
            String v2 = "OFF";
            mySQLiteAdapter.insert(v1, v2);
            updateList();
        }

       };



       OnClickListener landoffOnClickListener = new Button.OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

            int id=25;
                mySQLiteAdapter.delete_byID(id);

                String v1="Landscape ";
                String v2 = "OFF";
                mySQLiteAdapter.insert(v1, v2);
                updateList();
            }

           };


           OnClickListener histoffOnClickListener = new Button.OnClickListener(){

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub

                    int id=27;
                mySQLiteAdapter.delete_byID(id);

                    String v1="Historical Site ";
                    String v2 = "OFF";
                    mySQLiteAdapter.insert(v1, v2);
                    updateList();
                }

               };

               OnClickListener muraloffOnClickListener = new Button.OnClickListener(){

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub

                    int id=23;
            mySQLiteAdapter.delete_byID(id);

                        String v1="Mural ";
                        String v2 = "OFF";
                        mySQLiteAdapter.insert(v1, v2);
                        updateList();
                    }

                   };



    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        mySQLiteAdapter.close();
    }

private void updateList() {
    // TODO Auto-generated method stub
    cursor.requery();
}
}

SQLiteAdapter.java class

package com.paad.databaseC;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.view.View;

public class SQLiteAdapter {

    public static final String MYDATABASE_NAME = "MY_DATABASE";
    public static final String MYDATABASE_TABLE = "ARC_TABLE";
    public static final int MYDATABASE_VERSION = 1;
    public static final String KEY_ID = "_id";
    public static final String KEY_CONTENT1 = "ArtType";
    public static final String KEY_CONTENT2 = "ArtMode";

    //create table MY_DATABASE (ID integer primary key, Content text not null);
    private static final String SCRIPT_CREATE_DATABASE =
        "create table " + MYDATABASE_TABLE + " ("
        + KEY_ID + " integer primary key autoincrement, "
        + KEY_CONTENT1 + " text not null, "
        + KEY_CONTENT2 + " text not null);";

    private SQLiteHelper sqLiteHelper;
    private SQLiteDatabase sqLiteDatabase;

    private Context context;

    public SQLiteAdapter(Context c){
        context = c;
    }

    public SQLiteAdapter openToRead() throws android.database.SQLException {
        sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
        sqLiteDatabase = sqLiteHelper.getReadableDatabase();
        return this;    
    }

    public SQLiteAdapter openToWrite() throws android.database.SQLException {
        sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
        sqLiteDatabase = sqLiteHelper.getWritableDatabase();
        return this;    
    }

    public void close(){
        sqLiteHelper.close();
    }

    public long insert(String ArtType, String ArtMode){

        ContentValues contentValues = new ContentValues();
        contentValues.put(KEY_CONTENT1, ArtType);
        contentValues.put(KEY_CONTENT2, ArtMode);
        return sqLiteDatabase.insert(MYDATABASE_TABLE, null, contentValues);
    }

    public int deleteAll(){
        return sqLiteDatabase.delete(MYDATABASE_TABLE, null, null);
    }



/*  public void updateAll(){
        sqLiteDatabase.update(MYDATABASE_TABLE, null, KEY_CONTENT1='vennila', null);

    }*/
    public void update_byID(String id, String v1, String v2){
        ContentValues values = new ContentValues();
        values.put(KEY_CONTENT1, v1);
        values.put(KEY_CONTENT2, v2);
        sqLiteDatabase.update(MYDATABASE_TABLE, values, KEY_ID+"="+id, null);
    }

    public Cursor queueAll(){
        String[] columns = new String[]{KEY_ID, KEY_CONTENT1, KEY_CONTENT2};
        Cursor cursor = sqLiteDatabase.query(MYDATABASE_TABLE, columns, 
                null, null, null, null, null);

        return cursor;
    }

    public class SQLiteHelper extends SQLiteOpenHelper {

        public SQLiteHelper(Context context, String name,
                CursorFactory factory, int version) {
            super(context, name, factory, version);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            // TODO Auto-generated method stub
            db.execSQL(SCRIPT_CREATE_DATABASE);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // TODO Auto-generated method stub

        }

    }

    public void updateAll(String v1, String v2) {
        // TODO Auto-generated method stub
        ContentValues values = new ContentValues();
        values.put(KEY_CONTENT1, v1);
        values.put(KEY_CONTENT2, v2);
        sqLiteDatabase.update(MYDATABASE_TABLE, values, KEY_CONTENT1, null);
    }


    public void delete_byID(int id) {
        // TODO Auto-generated method stub
        sqLiteDatabase.delete(MYDATABASE_TABLE, KEY_CONTENT1+"="+id, null);
    }




}
</code></pre>
