简体   繁体   English

如何从数据库中删除选定的列表视图项

[英]how to delete selected listview item from database

how to delete listview selected possition id value from database my code just only listview position value which is not equal to database value i want to delete listview selected position value from listview also from database and show KEY_TIME value of selected item in toast how to do that?? 如何从数据库中删除listview选定的位置id值我的代码仅是listview位置值,它不等于数据库值我也想从数据库中从listview删除listview选定的位置值,并在烤面包中显示选定项的KEY_TIME值怎么做??

          msglist.setOnItemLongClickListener(new OnItemLongClickListener() {
   // setting onItemLongClickListener and passing the position to the function
          @Override
  public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
        int position, long arg3) {

              DatabaseHandler db = new DatabaseHandler(MsgActivity.this);


        Log.d("LOGInG VALUE", "Value: " + String.valueOf(arg3));


                      db.Delete_Contact(position);



              return true;
   }
 });


   public class DatabaseHandler extends SQLiteOpenHelper {

    String CREATE_INBOX_TABLE = "CREATE TABLE " + TABLE_INBOX + "("
+ KEY_INBOXID + " INTEGER PRIMARY KEY," + KEY_MSG + " TEXT,"+ KEY_TIME + " TEXT" + ")";
public void Delete_Contact(int id) {
//   String string =String.valueOf(id);
    SQLiteDatabase db = this.getWritableDatabase();
    db.delete(TABLE_INBOX, KEY_INBOXID + " =? ",  new String[] { 
       String.valueOf(id) });

            db.close();
}


    }
  DataBaseManager  data = new DataBaseManager(context.getApplicationContext());

                 String QUERY = "SELECT * FROM Cart Where id= "your id";

                    Cursor  cursor = data.selectQuery(QUERY);
    int count = cursor.getCount();
                   if (count <= 0) {

                }
             else {
                data.Delete("YOUR DATABASE NAME", "YOUR ID");
                list.remove(position);
                notifyDataSetChanged();
                }

DatabaseMangerClass DatabaseMangerClass

        public class DataBaseManager extends SQLiteOpenHelper {

            // The Android's default system path of your application database.

            @SuppressLint("SdCardPath")
            private static String DB_PATH = "data/data/com.Salsoft.pharmapacks/";
            private static String DB_NAME = "Cart.sqlite";
            private SQLiteDatabase myDataBase;
            private SQLiteDatabase myData;
            private Context myContext;

            // /data/data/com.salsoft.savingdata/db/SavingData.sqlite

            /**
             * Constructor Takes and keeps a reference of the passed context in order to
             * access to the application assets and resources.
             * 
             * @param context
             */
            public DataBaseManager(Context context) {
                super(context, DB_NAME, null, 1);
                this.myContext = context;
                Boolean isSDPresent = android.os.Environment.getExternalStorageState()
                        .equals(android.os.Environment.MEDIA_MOUNTED);

                if (isSDPresent) {
                    // yes SD-card is present
                } else {
                    // Sorry
                }
            }

            /**
             * Creates a empty database on the system and rewrites it with your own
             * database.
             * */
            public void createDataBase() throws IOException {

                boolean dbExist = checkDataBase();
                if (dbExist) {
                    // do nothing - database already exist
                } else {
                    File directory = new File(DB_PATH);
                    directory.mkdirs();
                    CopyFiles();
                }
            }

            private void CopyFiles() {
                try {
                    InputStream is = myContext.getAssets().open(DB_NAME);
                    File outfile = new File(DB_PATH, DB_NAME);
                    outfile.getParentFile().mkdirs();
                    outfile.createNewFile();

                    if (is == null) {
                        throw new RuntimeException("stream is null");
                    } else {
                        FileOutputStream out = new FileOutputStream(outfile);
                        byte buf[] = new byte[128];
                        do {
                            int numread = is.read(buf);
                            if (numread <= 0)
                                break;
                            out.write(buf, 0, numread);
                        } while (true);

                        is.close();
                        out.close();
                    }

                } catch (IOException e) {
                    throw new RuntimeException(e);
                }

            }

            /**
             * Check if the database already exist to avoid re-copying the file each
             * time you open the application.
             * 
             * @return true if it exists, false if it doesn't
             */
            private boolean checkDataBase() {

                SQLiteDatabase checkDB = null;

                try {
                    String myPath = DB_PATH + DB_NAME;
                    checkDB = SQLiteDatabase.openDatabase(myPath, null,
                            SQLiteDatabase.OPEN_READWRITE);

                } catch (SQLiteException e) {

                }

                if (checkDB != null) {
                    checkDB.close();
                }

                return checkDB != null ? true : false;
            }

            public void openDataBase() throws SQLException {

                // Open the database
                String myPath = DB_PATH + DB_NAME;
                myDataBase = SQLiteDatabase.openDatabase(myPath, null,
                        SQLiteDatabase.OPEN_READWRITE);
            }

            @Override
            public synchronized void close() {
                if (myDataBase != null)
                    myDataBase.close();
                super.close();
            }

            public void insert(String table, String num, ContentValues content) {
                String myPath = DB_PATH + DB_NAME;

                myData = SQLiteDatabase.openDatabase(myPath, null,
                        SQLiteDatabase.OPEN_READWRITE);
                myData.insert(table, num, content);

            }

            public void update(String tablename, ContentValues content, String productid) {
                String myPath = DB_PATH + DB_NAME;
                myData = SQLiteDatabase.openDatabase(myPath, null,
                        SQLiteDatabase.OPEN_READWRITE);
                myData.update(tablename, content, "productid = ?",
                        new String[] { productid });

            }

            public void Delete(String tablename, String productid) {
                String myPath = DB_PATH + DB_NAME;
                myData = SQLiteDatabase.openDatabase(myPath, null,
                        SQLiteDatabase.OPEN_READWRITE);

                myData.delete(tablename, "productid = ?", new String[] { productid });

            }

            @Override
            public void onCreate(SQLiteDatabase db) {
            }

            @Override
            public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            }

            // ---retrieve records---
            public Cursor selectQuery(String query) throws SQLException {
                String myPath = DB_PATH + DB_NAME;
                myData = SQLiteDatabase.openDatabase(myPath, null,
                        SQLiteDatabase.OPEN_READONLY);
                Cursor mCursor = myData.rawQuery(query, null);
                mCursor.moveToFirst();
                myData.close();
                return mCursor;
            }

            // //////// For Insert And Update Data ////////
            public void insert_update(String query) throws SQLException {
                String myPath = DB_PATH + DB_NAME;
                myData = SQLiteDatabase.openDatabase(myPath, null,
                        SQLiteDatabase.OPEN_READWRITE);
                myData.execSQL(query);
                myData.close();
            }

        }

Add this to your DatabaseManager : 将此添加到您的DatabaseManager

public void DeleteByName(String Name){

    SQLiteDatabase db = this.getWritableDatabase();
    db.delete(tableName , columnName , selectionArgs);
    db.close();

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM