Android Delete Rows Data in SQLite
package com.myapp; import java.util.ArrayList; import java.util.HashMap; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; public class myDBClass extends SQLiteOpenHelper { // Database Version private static final int DATABASE_VERSION = 1; // Database Name private static final String DATABASE_NAME = "mydatabase"; // Table Name private static final String TABLE_MEMBER = "members"; public myDBClass(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); // TODO Auto-generated constructor stub } @Override public void onCreate(SQLiteDatabase db) { // TODO Auto-generated method stub // Create Table Name db.execSQL("CREATE TABLE " + TABLE_MEMBER + "(MemberID INTEGER PRIMARY KEY AUTOINCREMENT," + " Name TEXT(100)," + " Tel TEXT(100));"); Log.d("CREATE TABLE","Create Table Successfully."); } // Delete Data public long DeleteData(String strMemberID) { // TODO Auto-generated method stub try { SQLiteDatabase db; db = this.getWritableDatabase(); // Write Data /** * for API 11 and above SQLiteStatement insertCmd; String strSQL = "DELETE FROM " + TABLE_MEMBER + " WHERE MemberID = ? "; insertCmd = db.compileStatement(strSQL); insertCmd.bindString(1, strMemberID); return insertCmd.executeUpdateDelete(); * */ long rows = db.delete(TABLE_MEMBER, "MemberID = ?", new String[] { String.valueOf(strMemberID) }); db.close(); return rows; // return rows deleted. } catch (Exception e) { return -1; } } // Show All Data public ArrayList<HashMap<String, String>> SelectAllData() { // TODO Auto-generated method stub try { ArrayList<HashMap<String, String>> MyArrList = new ArrayList<HashMap<String, String>>(); HashMap<String, String> map; SQLiteDatabase db; db = this.getReadableDatabase(); // Read Data String strSQL = "SELECT * FROM " + TABLE_MEMBER; Cursor cursor = db.rawQuery(strSQL, null); if(cursor != null) { if (cursor.moveToFirst()) { do { map = new HashMap<String, String>(); map.put("MemberID", cursor.getString(0)); map.put("Name", cursor.getString(1)); map.put("Tel", cursor.getString(2)); MyArrList.add(map); } while (cursor.moveToNext()); } } cursor.close(); db.close(); return MyArrList; } catch (Exception e) { return null; } } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // TODO Auto-generated method stub db.execSQL("DROP TABLE IF EXISTS " + TABLE_MEMBER); // Re Create on method onCreate onCreate(db); } }
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="35dp" android:text="Main Menu" android:textAppearance="?android:attr/textAppearanceLarge" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textView1" android:layout_centerHorizontal="true" android:layout_marginTop="35dp" android:text="Add" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/button1" android:layout_centerInParent="true" android:layout_marginTop="35dp" android:text="Show" /> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/button2" android:layout_centerInParent="true" android:layout_marginTop="35dp" android:text="Update" /> <Button android:id="@+id/button4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/button3" android:layout_centerInParent="true" android:layout_marginTop="35dp" android:text="Delete" /> </RelativeLayout>
package com.myapp; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.app.Activity; import android.content.Intent; public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Button4 (Delete) final Button btn4 = (Button) findViewById(R.id.button4); // Perform action on click btn4.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // Open Form ListDelete Intent newActivity = new Intent(MainActivity.this,ListDeleteActivity.class); startActivity(newActivity); } }); } }
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/tableLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TableRow android:id="@+id/tableRow1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="5dip" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:gravity="center" android:text="Delete Member : " android:layout_span="1" android:textAppearance="?android:attr/textAppearanceLarge" /> </TableRow> <View android:layout_height="1dip" android:background="#CCCCCC" /> <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="0.1"> <ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:focusable="false" android:focusableInTouchMode="false" android:clickable="false"> </ListView> </LinearLayout> <View android:layout_height="1dip" android:background="#CCCCCC" /> <LinearLayout android:id="@+id/LinearLayout1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="5dip" > <Button android:id="@+id/btnCancel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Cancel" /> </LinearLayout> </TableLayout>
<resources> <string name="app_name">MyApp</string> <string name="hello_world">Hello world!</string> <string name="menu_settings">Settings</string> <string name="title_activity_main">My App V 1.0</string> <string-array name="CmdMenu"> <item>Edit</item> <item>Delete</item> </string-array> </resources>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/linearLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/ColMemberID" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="MemberID"/> <TextView android:id="@+id/ColName" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="2" android:text="Name"/> <TextView android:id="@+id/ColTel" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="Tel" /> </LinearLayout>
package com.myapp; import java.util.ArrayList; import java.util.HashMap; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.ContextMenu; import android.view.ContextMenu.ContextMenuInfo; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.AdapterView; import android.widget.Button; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget.Toast; public class ListDeleteActivity extends Activity { ArrayList<HashMap<String, String>> MebmerList; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_list_delete); // Call Show List All Data ShowListData(); // btnCancel (Cancel) final Button cancel = (Button) findViewById(R.id.btnCancel); cancel.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // Open Form Main Intent newActivity = new Intent(ListDeleteActivity.this,MainActivity.class); startActivity(newActivity); } }); } // Show List data public void ShowListData() { myDBClass myDb = new myDBClass(this); MebmerList = myDb.SelectAllData(); // listView1 ListView lisView1 = (ListView)findViewById(R.id.listView1); SimpleAdapter sAdap; sAdap = new SimpleAdapter(ListDeleteActivity.this, MebmerList, R.layout.activity_column, new String[] {"MemberID", "Name", "Tel"}, new int[] {R.id.ColMemberID, R.id.ColName, R.id.ColTel}); lisView1.setAdapter(sAdap); registerForContextMenu(lisView1); } @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { //if (v.getId()==R.id.list) { AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo; menu.setHeaderTitle("Command for : " + MebmerList.get(info.position).get("Name").toString()); String[] menuItems = getResources().getStringArray(R.array.CmdMenu); for (int i = 0; i<menuItems.length; i++) { menu.add(Menu.NONE, i, i, menuItems[i]); } //} } @Override public boolean onContextItemSelected(MenuItem item) { AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo(); int menuItemIndex = item.getItemId(); String[] menuItems = getResources().getStringArray(R.array.CmdMenu); String CmdName = menuItems[menuItemIndex]; String MemID = MebmerList.get(info.position).get("MemberID").toString(); //String MemName = MebmerList.get(info.position).get("Name").toString(); // Check Event Command if ("Edit".equals(CmdName)) { // Show on new activity //Intent newActivity = new Intent(ListDeleteActivity.this,UpdateActivity.class); //newActivity.putExtra("MemID", MebmerList.get(info.position).get("ID").toString()); //startActivity(newActivity); // for Delete Command } else if ("Delete".equals(CmdName)) { myDBClass myDb = new myDBClass(this); long flg = myDb.DeleteData(MemID); if(flg > 0) { Toast.makeText(ListDeleteActivity.this,"Delete Data Successfully", Toast.LENGTH_LONG).show(); } else { Toast.makeText(ListDeleteActivity.this,"Delete Data Failed.", Toast.LENGTH_LONG).show(); } // Call Show Data again ShowListData(); } return true; } }
<activity android:name="ListDeleteActivity" android:theme="@style/AppTheme" android:screenOrientation="portrait" android:label="@string/title_activity_main" />
if ("Edit".equals(CmdName)) { // Show on new activity Intent newActivity = new Intent(ListDeleteActivity.this,UpdateActivity.class); newActivity.putExtra("MemID", MebmerList.get(info.position).get("ID").toString()); startActivity(newActivity); }
Kaynak : Google