user3332060
user3332060

Reputation: 19

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??

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


    }

Upvotes: 0

Views: 1461

Answers (2)

user3516009
user3516009

Reputation: 28

Add this to your DatabaseManager:

public void DeleteByName(String Name){

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

}

Upvotes: 0

Ammar ali
Ammar ali

Reputation: 1503

  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

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

        }

Upvotes: 1

Related Questions