如何从SQLite数据库中删除特定行

问题描述:

我已经创建了一个列表视图.在按钮上单击,数据将添加到列表中,并且也存储在SQLite数据库中.我还在列表的右侧添加了图像,单击该图像会删除特定的行数据,但是我也想从SQLite中删除数据.我该怎么办?

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;
    }

}

要删除单个数据条目(即行),请在数据库处理程序中创建一个方法:

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();
    } 

此方法将删除给出ID的单行.

This method will delete single row which ID is given.