Friday, May 23, 2014

Delete row in SQLite database

android.database.sqlite.SQLiteDatabase class provide a convenience method for deleting rows in the database.




Modify SQLiteAdapter.java, implement a method delete_byID(int id) to delete individual row using id.
  1. package com.exercise.AndroidSQLite;
  2.  
  3. import android.content.ContentValues;
  4. import android.content.Context;
  5. import android.database.Cursor;
  6. import android.database.sqlite.SQLiteDatabase;
  7. import android.database.sqlite.SQLiteOpenHelper;
  8. import android.database.sqlite.SQLiteDatabase.CursorFactory;
  9.  
  10. public class SQLiteAdapter {
  11.  
  12. public static final String MYDATABASE_NAME = "MY_DATABASE";
  13. public static final String MYDATABASE_TABLE = "MY_TABLE";
  14. public static final int MYDATABASE_VERSION = 1;
  15. public static final String KEY_ID = "_id";
  16. public static final String KEY_CONTENT1 = "Content1";
  17. public static final String KEY_CONTENT2 = "Content2";
  18.  
  19. //create table MY_DATABASE (ID integer primary key, Content text not null);
  20. private static final String SCRIPT_CREATE_DATABASE =
  21. "create table " + MYDATABASE_TABLE + " ("
  22. + KEY_ID + " integer primary key autoincrement, "
  23. + KEY_CONTENT1 + " text not null, "
  24. + KEY_CONTENT2 + " text not null);";
  25.  
  26. private SQLiteHelper sqLiteHelper;
  27. private SQLiteDatabase sqLiteDatabase;
  28.  
  29. private Context context;
  30.  
  31. public SQLiteAdapter(Context c){
  32. context = c;
  33. }
  34.  
  35. public SQLiteAdapter openToRead() throws android.database.SQLException {
  36. sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
  37. sqLiteDatabase = sqLiteHelper.getReadableDatabase();
  38. return this;
  39. }
  40.  
  41. public SQLiteAdapter openToWrite() throws android.database.SQLException {
  42. sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
  43. sqLiteDatabase = sqLiteHelper.getWritableDatabase();
  44. return this;
  45. }
  46.  
  47. public void close(){
  48. sqLiteHelper.close();
  49. }
  50.  
  51. public long insert(String content1, String content2){
  52.  
  53. ContentValues contentValues = new ContentValues();
  54. contentValues.put(KEY_CONTENT1, content1);
  55. contentValues.put(KEY_CONTENT2, content2);
  56. return sqLiteDatabase.insert(MYDATABASE_TABLE, null, contentValues);
  57. }
  58.  
  59. public int deleteAll(){
  60. return sqLiteDatabase.delete(MYDATABASE_TABLE, null, null);
  61. }
  62.  
  63. public void delete_byID(int id){
  64. sqLiteDatabase.delete(MYDATABASE_TABLE, KEY_ID+"="+id, null);
  65. }
  66.  
  67. public Cursor queueAll(){
  68. String[] columns = new String[]{KEY_ID, KEY_CONTENT1, KEY_CONTENT2};
  69. Cursor cursor = sqLiteDatabase.query(MYDATABASE_TABLE, columns,
  70. null, null, null, null, null);
  71.  
  72. return cursor;
  73. }
  74.  
  75. public class SQLiteHelper extends SQLiteOpenHelper {
  76.  
  77. public SQLiteHelper(Context context, String name,
  78. CursorFactory factory, int version) {
  79. super(context, name, factory, version);
  80. }
  81.  
  82. @Override
  83. public void onCreate(SQLiteDatabase db) {
  84. // TODO Auto-generated method stub
  85. db.execSQL(SCRIPT_CREATE_DATABASE);
  86. }
  87.  
  88. @Override
  89. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  90. // TODO Auto-generated method stub
  91.  
  92. }
  93. }
  94. }
Modify AndroidSQLite.java to implement a custom AlertDialog to handle the ui, call delete_byID(int id) to delete row.
  1. package com.exercise.AndroidSQLite;
  2.  
  3. import android.app.Activity;
  4. import android.app.AlertDialog;
  5. import android.content.DialogInterface;
  6. import android.database.Cursor;
  7. import android.os.Bundle;
  8. import android.view.View;
  9. import android.view.ViewGroup.LayoutParams;
  10. import android.widget.AdapterView;
  11. import android.widget.Button;
  12. import android.widget.EditText;
  13. import android.widget.LinearLayout;
  14. import android.widget.ListView;
  15. import android.widget.SimpleCursorAdapter;
  16. import android.widget.TextView;
  17.  
  18. public class AndroidSQLite extends Activity {
  19.  
  20. EditText inputContent1, inputContent2;
  21. Button buttonAdd, buttonDeleteAll;
  22.  
  23. private SQLiteAdapter mySQLiteAdapter;
  24. ListView listContent;
  25.  
  26. SimpleCursorAdapter cursorAdapter;
  27. Cursor cursor;
  28.  
  29. /** Called when the activity is first created. */
  30. @Override
  31. public void onCreate(Bundle savedInstanceState) {
  32. super.onCreate(savedInstanceState);
  33. setContentView(R.layout.main);
  34. inputContent1 = (EditText)findViewById(R.id.content1);
  35. inputContent2 = (EditText)findViewById(R.id.content2);
  36. buttonAdd = (Button)findViewById(R.id.add);
  37. buttonDeleteAll = (Button)findViewById(R.id.deleteall);
  38. listContent = (ListView)findViewById(R.id.contentlist);
  39.  
  40. mySQLiteAdapter = new SQLiteAdapter(this);
  41. mySQLiteAdapter.openToWrite();
  42.  
  43. cursor = mySQLiteAdapter.queueAll();
  44. String[] from = new String[]{SQLiteAdapter.KEY_ID, SQLiteAdapter.KEY_CONTENT1, SQLiteAdapter.KEY_CONTENT2};
  45. int[] to = new int[]{R.id.id, R.id.text1, R.id.text2};
  46. cursorAdapter =
  47. new SimpleCursorAdapter(this, R.layout.row, cursor, from, to);
  48. listContent.setAdapter(cursorAdapter);
  49. listContent.setOnItemClickListener(listContentOnItemClickListener);
  50. buttonAdd.setOnClickListener(buttonAddOnClickListener);
  51. buttonDeleteAll.setOnClickListener(buttonDeleteAllOnClickListener);
  52. }
  53.  
  54. Button.OnClickListener buttonAddOnClickListener
  55. = new Button.OnClickListener(){
  56.  
  57. @Override
  58. public void onClick(View arg0) {
  59. // TODO Auto-generated method stub
  60. String data1 = inputContent1.getText().toString();
  61. String data2 = inputContent2.getText().toString();
  62. mySQLiteAdapter.insert(data1, data2);
  63. updateList();
  64. }
  65. };
  66.  
  67. Button.OnClickListener buttonDeleteAllOnClickListener
  68. = new Button.OnClickListener(){
  69.  
  70. @Override
  71. public void onClick(View arg0) {
  72. // TODO Auto-generated method stub
  73. mySQLiteAdapter.deleteAll();
  74. updateList();
  75. }
  76. };
  77.  
  78. private ListView.OnItemClickListener listContentOnItemClickListener
  79. = new ListView.OnItemClickListener(){
  80.  
  81. @Override
  82. public void onItemClick(AdapterView parent, View view, int position,
  83. long id) {
  84. // TODO Auto-generated method stub
  85. Cursor cursor = (Cursor) parent.getItemAtPosition(position);
  86. final int item_id = cursor.getInt(cursor.getColumnIndex(SQLiteAdapter.KEY_ID));
  87. String item_content1 = cursor.getString(cursor.getColumnIndex(SQLiteAdapter.KEY_CONTENT1));
  88. String item_content2 = cursor.getString(cursor.getColumnIndex(SQLiteAdapter.KEY_CONTENT2));
  89. AlertDialog.Builder myDialog
  90. = new AlertDialog.Builder(AndroidSQLite.this);
  91. myDialog.setTitle("Delete?");
  92. TextView dialogTxt_id = new TextView(AndroidSQLite.this);
  93. LayoutParams dialogTxt_idLayoutParams
  94. = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
  95. dialogTxt_id.setLayoutParams(dialogTxt_idLayoutParams);
  96. dialogTxt_id.setText("#" + String.valueOf(item_id));
  97. TextView dialogC1_id = new TextView(AndroidSQLite.this);
  98. LayoutParams dialogC1_idLayoutParams
  99. = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
  100. dialogC1_id.setLayoutParams(dialogC1_idLayoutParams);
  101. dialogC1_id.setText(item_content1);
  102. TextView dialogC2_id = new TextView(AndroidSQLite.this);
  103. LayoutParams dialogC2_idLayoutParams
  104. = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
  105. dialogC2_id.setLayoutParams(dialogC2_idLayoutParams);
  106. dialogC2_id.setText(item_content2);
  107. LinearLayout layout = new LinearLayout(AndroidSQLite.this);
  108. layout.setOrientation(LinearLayout.VERTICAL);
  109. layout.addView(dialogTxt_id);
  110. layout.addView(dialogC1_id);
  111. layout.addView(dialogC2_id);
  112. myDialog.setView(layout);
  113. myDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
  114. // do something when the button is clicked
  115. public void onClick(DialogInterface arg0, int arg1) {
  116. mySQLiteAdapter.delete_byID(item_id);
  117. updateList();
  118. }
  119. });
  120. myDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
  121. // do something when the button is clicked
  122. public void onClick(DialogInterface arg0, int arg1) {
  123. }
  124. });
  125. myDialog.show();
  126. }};
  127.  
  128. @Override
  129. protected void onDestroy() {
  130. // TODO Auto-generated method stub
  131. super.onDestroy();
  132. mySQLiteAdapter.close();
  133. }
  134.  
  135. private void updateList(){
  136. cursor.requery();
  137. }
  138. }
All other files, main.xml and row.xml, follow the last exercise.

No comments:

Post a Comment