selectDb(); if(selectFavoriteState()){ favorite.setImageResource(R.drawable.ic_favorite_like); }else { favorite.setImageResource(R.drawable.ic_favorite_maylike); } favorite.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (selectFavoriteState()){ favorite.setImageResource(R.drawable.ic_favorite_maylike); updateUnfavorited(); }else { favorite.setImageResource(R.drawable.ic_favorite_like); updateFavorited(); } } }); private void selectDb(){ destpath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/ielts/"; database = SQLiteDatabase.openOrCreateDatabase(destpath + "/md_book.db", null); } private boolean selectFavoriteState(){ @SuppressLint("Recycle") Cursor cursor = database.rawQuery("SELECT * FROM main WHERE id = " + id, null); while (cursor.moveToNext()){ favoriteState = cursor.getString(cursor.getColumnIndex("fav")); } return favoriteState.equals("1"); } private void updateFavorited(){ database.execSQL( "UPDATE main SET fav = 1 WHERE id = " + id); } private void updateUnfavorited(){ database.execSQL( "UPDATE main SET fav = 0 WHERE id = " + id); }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)