I am using Room + View Model
i want to check if the ID for the movie exist in the DB or not , in order to know if the user added it to the favourite or not to change the button colour and text and to remove it from DB if the button clicked again.
I don't know how to implement if statement to implement it , i tried a lot of things but it doesn't work.
this is My MovieDetailActivity
public class MovieActivity extends AppCompatActivity {
private RecyclerView.LayoutManager rvLayout;
public Context context;
// Constant for default task id to be used when not in favourite
private static final int DEFAULT_TASK_ID = -1;
ToggleButton button;
AppDatabase appDatabase;
Movie movie;
int id;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.movies_details);
appDatabase = AppDatabase.getInstance(getApplicationContext());
Intent intent = getIntent();
movie = intent.getParcelableExtra("get_data");
id = movie.getId();
checkFavorite();
if (id != DEFAULT_TASK_ID) {
AddTaskViewModelFactory factory = new AddTaskViewModelFactory(appDatabase, id);
final AddTaskViewModel viewModel = ViewModelProviders.of(this, factory).get(AddTaskViewModel.class);
viewModel.getTask().observe(this, new Observer<Movie>() {
@Override
public void onChanged(Movie movie) {
viewModel.getTask().removeObserver(this);
button.setTextOn("Added");
button.getResources().getColor(R.color.colorGreen);
}
});
}
button.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (b) {
compoundButton.setBackgroundColor(ContextCompat.getColor(compoundButton.getContext(), R.color.colorGreen));
addToFavourite();
} else {
compoundButton.setBackgroundColor(ContextCompat.getColor(compoundButton.getContext(), R.color.colorGrey));
deleteFromFavourite();
}
}
});
}
public void addToFavourite() {
AppExecutors.getInstance().diskIO().execute(() -> {
appDatabase.movieDao().insert(movie);
Log.d("the movie ID added " + id, " good");
});
}
public void deleteFromFavourite() {
AppExecutors.getInstance().diskIO().execute(() -> {
appDatabase.movieDao().delete(movie);
Log.d("the movie ID deleted " + id, " bad");
});
}
public void checkFavorite() {
AppExecutors.getInstance().diskIO().execute(() -> {
appDatabase.movieDao().getAllMovies(id);
if (appDatabase.movieDao().check(id) != 0) {
button.setTextOn("Added");
button.getResources().getColor(R.color.colorGreen);
}
Log.d("the movie ID deleted " + id, " bad");
});
}
}
this is my Dao
@Dao
public interface MovieDao {
@Query("SELECT * FROM Movies_Table WHERE id = :id")
LiveData<Movie> getAllMovies(int id);
@Update()
void update(Movie m);
@Query("SELECT id FROM Movies_Table WHERE id = :id")
int check(int id);
@Insert(onConflict = OnConflictStrategy.REPLACE)
void insert(Movie m);
@Delete()
void delete(Movie m);
@Query("SELECT * FROM Movies_Table ")
LiveData<List<Movie>> getAllMovies();
}
Try changing the DAOs check
method:
@Query("SELECT count(id) FROM Movies_Table WHERE id = :id")
int check(int id);
and it will return 0 if the movie with id doesn't exists and non-zero if it exists in the table
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.