How to delete a particular row from SQLite database

纵饮孤独 提交于 2019-12-25 16:43:18

问题


I have created a list view. On button click the data is added in list and also stored in an SQLite database. I have also added the image on the right-hand side of list, on click of which a particular row data is deleted, but I also want to delete data from SQLite. How could I do this?

Main Activity.Java

public class MainActivity extends Activity {

       EditText editText;
        Button Button,Button1;
        ListView listView;
        ArrayList<String> listItems;
        BaseAdapter adapter;
        private DataBaseHandler mHelper;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
           mHelper=new DataBaseHandler(this);
          editText = (EditText) findViewById(R.id.editText);

            Button = (Button) findViewById(R.id.Button);
            listView = (ListView) findViewById(R.id.listview);  

            listItems = new ArrayList<String>();

            adapter =new BaseAdapter() 
            {

                @Override
                public View getView(final int arg0, View arg1, ViewGroup arg2)
                {
                LayoutInflater inflater = getLayoutInflater();
                arg1 = inflater.inflate(R.layout.custom, null);
                TextView textview = (TextView)arg1. findViewById(R.id.textView1);
                textview.setText(listItems.get(arg0));
                ImageView image = (ImageView)arg1. findViewById(R.id.imageView1);

                image.setOnClickListener(new OnClickListener()
                {

                @Override
                public void onClick(View v)
                {

                listItems.remove(arg0);

                listView.setAdapter(adapter);   
                Toast.makeText(MainActivity.this, "Item has been successfully deleted", Toast.LENGTH_LONG)
                .show();

                }
                });

                return arg1;
                }

                @Override
                public long getItemId(int arg0) {

                return 0;
                }

                @Override
                public Object getItem(int arg0) {

                return null;
                }

                @Override
                public int getCount() {
                //code to set the list item size according to array data size
                return listItems.size();
                }
                };

                listView.setAdapter(adapter);
                Button.setOnClickListener(new View.OnClickListener() {

                public void onClick(View v)
                {

                listItems=  mHelper.addListItem(editText.getText().toString());

                adapter.notifyDataSetChanged();


                listView.setSelection(listView.getAdapter().getCount()-1);

                editText.getText().clear();
                }
                });

        }
    }





DATABASE HANDLER.JAVA

 public class DataBaseHandler extends SQLiteOpenHelper {

    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "MyDatabase.db";
     private static final String TABLE_LIST = "MyListItem";
    private static final String KEY_ID = "id";
    private static final String KEY_ListItem = "listitem";

    public static final String KEY_NAME = null;

    public DataBaseHandler(Context context) { 
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
    @Override
    public void onCreate(SQLiteDatabase db)
    {
    String CREATE_LIST_TABLE = "CREATE TABLE " + TABLE_LIST + "(" + KEY_ID
    + " integer primary key autoincrement," + KEY_ListItem + " TEXT" + ")";

    db.execSQL(CREATE_LIST_TABLE);
    }
   @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    db.execSQL("DROP TABLE IF EXISTS " + TABLE_LIST);

    onCreate(db);
    }
    ArrayList<String>  addListItem(String listItem)
    {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_ListItem, listItem);
    db.insert(TABLE_LIST, null, values);

    ArrayList<String> items=new ArrayList<String>();
    String selectQuery = "SELECT  * FROM " + TABLE_LIST;
    Cursor cursor = db.rawQuery(selectQuery, null);

    cursor.moveToFirst();


    while (!cursor.isAfterLast())
    {

    items.add(cursor.getString(1));
    cursor.moveToNext();

    }

    cursor.close();


    db.close(); 

    return items;
    }
    Cursor getListItem()
    {
    String selectQuery = "SELECT  * FROM " + TABLE_LIST;

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

        return cursor;
    }

}

回答1:


To delete single data entry(i.e. Row) make a method in database handler:

public void delete(int id) {

        SQLiteDatabase db = this.getWritableDatabase();
        db.delete(TABLE_NAME, KEY_ID + "=?",
                new String[] { String.valueOf(id) }); // KEY_ID= id of row and third parameter is argument.
        db.close();
    } 

This method will delete single row which ID is given.




回答2:


You will need to make a method deleteItem in your DataBaseHandler class. Which will do something like,

db.delete(TABLE_LIST , KEY_ID + "=" + yourIdToBeDeleted, null);

And make a call to this method from your delete button/image's onClick as:

mHelper.deleteItem(idOfItemToBeDeleted);

Hope it helps.



来源:https://stackoverflow.com/questions/26094054/how-to-delete-a-particular-row-from-sqlite-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!