0

I want to check whether the item is in the favorite or not. to show the icon favorite on a load of this activity. Please help me out, thanks in advance.

public boolean onCreateOptionsMenu(Menu menu) {
        this.menu =  menu;
        getMenuInflater().inflate(R.menu.favorite_menu,menu);
        Databasehelper databasehelper = new Databasehelper(this);
        SQLiteDatabase sqLiteDatabase= databasehelper.getReadableDatabase();
        // what should I do here ???
        return true;
    }

This is on click listener of the menu to update a favorite column in the database. I want to read that specific column in on create options menu.

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        int id=item.getItemId();
        if(id== R.id.favorites_button_unchecked){
            Databasehelper databasehelper = new Databasehelper(this);
            SQLiteDatabase sqLiteDatabase= databasehelper.getWritableDatabase();
            ContentValues values = new ContentValues();
            values.put(Movie.COLUMN_FAVORITE,"1");
            sqLiteDatabase.update(Movie.TABLE_NAME,values,Movie.COLUMN_ID + " = " + movie.getId(),null);
            Toast.makeText(this,"Movie Added to Favorite",Toast.LENGTH_LONG).show();
            menu.getItem(1).setVisible(true);
            menu.getItem(0).setVisible(false);
        }else if(id==R.id.favorites_button_checked){
            Databasehelper databasehelper = new Databasehelper(this);
            SQLiteDatabase sqLiteDatabase= databasehelper.getWritableDatabase();
            ContentValues values = new ContentValues();
            values.put(Movie.COLUMN_FAVORITE,"0");
            sqLiteDatabase.update(Movie.TABLE_NAME,values,Movie.COLUMN_ID + " = " + movie.getId(),null);
            Toast.makeText(this,"Movie Removed from Favorite",Toast.LENGTH_LONG).show();
            menu.getItem(0).setVisible(true);
            menu.getItem(1).setVisible(false);
        }

        return super.onOptionsItemSelected(item);
    }

1 Answer 1

1

Try This

String select_query = "SELECT * FROM " + TableName + " where " + idorwhatever +"= " value+" ";
    Cursor cursor = sqldb.rawQuery(select_query,null);
    // for each item
    if (cursor.moveToFirst()){
        do {
            // do stuff here, use cursor.getString(index)
        } while (cursor.moveToNext());
    }
Sign up to request clarification or add additional context in comments.

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.