简体   繁体   中英

Delete item from listview (using sqlite delete query)

I would like to delete item from my listview by ItemlongClickListener. display items are column value of my database. database queries are written in a separate file and listview code is in separate file. I tried but my app getting stopped.. My ListView page code are as follows:

fav_quote.setOnItemLongClickListener(new OnItemLongClickListener() {
        public boolean onItemLongClick(AdapterView<?> parent, View v, int position, long id) {
            Log.w("DELETED", " DELETED");

            String position1 =  adapt.getItem(position);

           Fav del = new Fav(this);
           del.deleteEntry(position1);
           del.close();
          return false;              
        }

Database Code

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        db.execSQL("DROP TABLE IF EXIST " + DATABASE_TABLE);
        onCreate(db);
    }       
}

public Fav(Context c) {

    ourContext = c;
}

public Fav(OnItemLongClickListener onItemLongClickListener) {
    Context c1 = null;
    ourContext = c1;
    // TODO Auto-generated constructor stub
}

public Fav open() throws SQLException {
ourHelper = new DbHelper(ourContext);
ourDatabase = ourHelper.getWritableDatabase();
return this; 
}
public void close (){

    ourHelper.close();
}

public long createEntry(String header, String quote_value) {
    // TODO Auto-generated method stub
    ContentValues cv = new ContentValues();
    cv.put(KEY_HEADER, header);
    cv.put(KEY_QUOTE_VALUE, quote_value);
    return ourDatabase.insert(DATABASE_TABLE, null, cv);
}

public String getData() {
    // TODO Auto-generated method stub
    String[] columns = new String []{KEY_ROWID, KEY_HEADER, KEY_QUOTE_VALUE};       

    Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
    String result = "";     
    int iRow = c.getColumnIndex(KEY_ROWID);
    int iHeader = c.getColumnIndex(KEY_HEADER);
    int iQuote_value = c.getColumnIndex(KEY_QUOTE_VALUE);

    for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
        result = result + c.getString(iRow)+ "." +c.getString(iHeader)+ ":" +c.getString(iQuote_value)+ ":";
    }       
    return result;              
}

public void deleteEntry(String position1) {
    // TODO Auto-generated method stub
    ourDatabase.delete(DATABASE_TABLE, KEY_ROWID + "=" + position1 , null);
}

Are the values deleted from Database? If yes then either remove that entry from your adaptor call method

    notifyDataSetChanged();

If no you have some problem with the database.

In deleteEntry class you have to initialize the sqlite like.

public void deleteEntry(String position1) {
// TODO Auto-generated method stub
 SQLiteDatabase ourDatabase = this.getWritableDatabase();
ourDatabase.delete(DATABASE_TABLE, KEY_ROWID + "=" + position1 , null);
}

尝试

Fav del = new Fav(NameOfActivityClass.this);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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