Search code examples
androidandroid-sqlite

Program won't reach while loop


I have an SQlite database and a list view that's populated with news articles. I can add articles to favorites just fine. When I try to delete it, it doesn't work.

I was following the tutorial on CodingWithMitch and made sure that my the SQl statements were identical but my program wont work.

Favorites Activity

//Deleting favorites
favoritesList.setOnItemLongClickListener(new 
AdapterView.OnItemLongClickListener() {
    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, 
int position, long id) {

    //final ArrayList<ListViewDetails> details = new ArrayList<>();
    //Cursor takes in a ListViewDetails object for a specific new article
    final Cursor cursor = databaseHelper.getID(details.get(position));

    //This is to test if the correct list view object is retrieved - Works
    Log.i("Test", details.get(position).link);

    //So the code won't reach this point and therefore nothing is executed
    int favoriteId = -1;  
    while (cursor.moveToNext()) {
        favoriteId = cursor.getInt(0);       
    }
        if (favoriteId > -1) {
            if (databaseHelper.delete(favoriteId) > 0) {

            Toast.makeText(getApplicationContext(), "Deleted", Toast.LENGTH_SHORT).show();
            Log.i("Delete", "It works!");
            adapter.remove(details.get(position));
            adapter.notifyDataSetChanged();     

            }
         }

DatabaseHelper class

public static final String COL0 = "ID";
public static final String COL1 = "TITLE";
public static final String COL2 = "PHOTO_URL";

@Override
public void onCreate(SQLiteDatabase db) {
    String sql = "CREATE TABLE " + TABLE_NAME + " ( " +
            COL0 + " INTEGER PRIMARY KEY AUTOINCREMENT, " 
...

public Cursor getID(ListViewDetails listViewDetails){
    SQLiteDatabase db = this.getWritableDatabase();
    String sql = "SELECT * FROM " + TABLE_NAME + " WHERE " +
            COL1 + " = '" + listViewDetails.getLink() + "'" +
            " AND " + COL2 + " = '" + listViewDetails.getTitle() + "'";       
    return db.rawQuery(sql,null);
}

public Integer delete(int id) {
    SQLiteDatabase db = this.getWritableDatabase();
    return db.delete(TABLE_NAME, "ID = ?", new String[] {String.valueOf(id)});
}

Just looking to find out why the code won't reach the while loop.


Solution

  • If there are no rows extracted then the while loop will finish without entering the body.

    This is the most likely scenario.

    Looking into why the Cursor might be empty. I believe that you have not matched the columns with the values in the getID method of the DatabaseHelper class.

    That is :-

     String sql = "SELECT * FROM " + TABLE_NAME + " WHERE " +
            COL1 + " = '" + listViewDetails.getLink() + "'" +
            " AND " + COL2 + " = '" + listViewDetails.getTitle() + "'";
    

    Should be :-

     String sql = "SELECT * FROM " + TABLE_NAME + " WHERE " +
            COL1 + " = '" + listViewDetails.getTitle() + "'" +
            " AND " + COL2 + " = '" + listViewDetails.getLink() + "'";
    

    i.e. COL1 is the Title and COL2 is the link

    • I'd suggest not using COL1 COl2 etc, but using descriptive names e.g. COL_TITLE and COL_LINK (such mistakes are the less likely to result).