繁体   English   中英

如何检查我的应用程序中电影的 ID 是否存在于我的数据库中

[英]How to check if the ID of a movie in my App exist in my DB or not

我正在使用房间 + 视图模型

我想检查电影的 ID 是否存在于数据库中,以了解用户是否将其添加到收藏夹中或不更改按钮颜色和文本,并在再次单击按钮时将其从数据库中删除。

我不知道如何实现 if 语句来实现它,我尝试了很多东西但它不起作用。

这是我的 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");
        });
    }

}

这是我的道

@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();

}

尝试更改 DAO check方法:

    @Query("SELECT count(id) FROM Movies_Table WHERE id = :id")
    int check(int id);

如果带有 id 的电影不存在,它将返回 0,如果它存在于表中,它将返回非零

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM