0

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

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

Try

Fav del = new Fav(NameOfActivityClass.this);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.