使用房间数据库添加到收藏夹

Add to favorite using room Database

我指的是本教程 ==> https://uniqueandrocode.com/add-to-favourites-and-display-favourites-in-recyclerview/ 在我的项目中我有底部导航...我正在尝试在第一个选项卡中添加收藏夹并在底部导航的第二个选项卡中显示收藏夹酒吧。我正在使用 Room 库。

当 activity 加载收藏夹时一开始都是空白的,但是当我将第一行作为收藏夹和 go-to 收藏夹选项卡时,它会正确显示,但是当我返回第一个选项卡时,它会填满所有自动收藏图标(我没做过我只做过第一行)

真的需要帮助。提前致谢。

道:

    @Dao
    public interface FavoriteDao {

    @Insert
    public void addData(FavoriteList favoriteList);

    @Query("select * from favoritelist")
    public List<FavoriteList> getFavoriteData();

    @Query("SELECT EXISTS (SELECT 1 FROM favoritelist WHERE id=:id)")
    public int isFavorite(int id);

    @Delete
    public void delete(FavoriteList favoriteList);

   }

数据库:

    @Database(entities={FavoriteList.class},version = 1)
    public abstract class FavoriteDatabase extends RoomDatabase {

    public abstract FavoriteDao favoriteDao();
    }

最喜欢的列表:

    @Entity(tableName="favoritelist")
     public class FavoriteList {


    @PrimaryKey
    private int id;

    @ColumnInfo(name = "source")
    private String source;

    @ColumnInfo(name = "author")
    private String author;

    @ColumnInfo(name = "title")
    private String title;


    @ColumnInfo(name = "description")
    private String description;

    @ColumnInfo(name = "url")
    private String url;
    @ColumnInfo(name = "urlToImage")
    private String urlToImage;
    @ColumnInfo(name = "publishedAt")
    private String publishedAt;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getSource() {
        return source;
    }

    public void setSource(String source) {
        this.source = source;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getUrlToImage() {
        return urlToImage;
    }

    public void setUrlToImage(String urlToImage) {
        this.urlToImage = urlToImage;
    }

    public String getPublishedAt() {
        return publishedAt;
    }

    public void setPublishedAt(String publishedAt) {
        this.publishedAt = publishedAt;
    }
}

新闻片段:

    public class news extends Fragment {
    ImageView favbtn;
    RecyclerView recyclerView;
    SwipeRefreshLayout swipeRefreshLayout;
    EditText etQuery;
    Button btnSearch;
   
    Adapter adapter;
    List<Articles> articles = new ArrayList<>();
    public static FavoriteDatabase favoriteDatabase;


    public news() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_news, container, false);
        swipeRefreshLayout = view.findViewById(R.id.swiprefresh);
        etQuery = view.findViewById(R.id.etQuery);
        btnSearch = view.findViewById(R.id.btnSearch);

        favoriteDatabase= Room.databaseBuilder(getActivity(),FavoriteDatabase.class,"myfavdb").
                allowMainThreadQueries().build();


        recyclerView = view.findViewById(R.id.recyclerview);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        final String country = getCountry();

        retrieveJson("", country, API_Key);

        btnSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!etQuery.getText().toString().equals("")) {
                    swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
                        @Override
                        public void onRefresh() {
                            retrieveJson(etQuery.getText().toString(), country, API_Key);
                        }
                    });
                    retrieveJson(etQuery.getText().toString(), country, API_Key);
                } else {

                    swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
                        @Override
                        public void onRefresh() {
                            retrieveJson("", country, API_Key);
                        }
                    });
                    retrieveJson("", country, API_Key);

                }
            }
        });
        return view;
    }

    private void showChangeLanguageDialog() {

    }

    public void retrieveJson(String query, String country, String apiKey) {

        swipeRefreshLayout.setRefreshing(true);
        Call<Headlines> call;
        if (!etQuery.getText().toString().equals("")) {
            call = ApiClient.getInstance().getApi().getSpecifiedData(query, apiKey);
        } else {
            call = ApiClient.getInstance().getApi().getHeadLines(country, apiKey);

        }


        call.enqueue(new Callback<Headlines>() {
            @Override
            public void onResponse(Call<Headlines> call, Response<Headlines> response) {
                if (response.isSuccessful() && response.body().getArticles() != null) {
                    swipeRefreshLayout.setRefreshing(false);
                   // articles.clear();
                    articles = response.body().getArticles();
                    adapter = new Adapter(getContext(), articles);
                    recyclerView.setAdapter(adapter);
                }
            }

            @Override
            public void onFailure(Call<Headlines> call, Throwable t) {
                swipeRefreshLayout.setRefreshing(false);
                Toast.makeText(getContext(), t.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
            }
        });

    }

    public String getCountry() {
        Locale locale = Locale.getDefault();
        String country = locale.getCountry();
        return country.toLowerCase();


    }

}

适配器:

    public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> {
    Context context;
    List<Articles> articles;

    public Adapter(Context context, List<Articles> articles) {
        this.context = context;
        this.articles = articles;
    }

    @NonNull
    @Override
    public Adapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final Adapter.ViewHolder holder, final int position) {
      final Articles a = articles.get(position);

        String imageUrl = a.getUrlToImage();
        String url = a.getUrl();

           holder.tvTitle.setText(a.getTitle());
        Picasso.get().load(imageUrl).into(holder.imageView);



        holder.tvSource.setText(a.getSource().getName());
        holder.tvDate.setText(dateTime(a.getPublishedAt()));

        holder.cardView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent intent = new Intent(context,DetailedActivity.class);
        intent.putExtra("title",a.getTitle());
        intent.putExtra("source",a.getSource().getName());
        intent.putExtra("time",dateTime(a.getPublishedAt()));
        intent.putExtra("desc",a.getDescription());
        intent.putExtra("imageUrl",a.getUrlToImage());
        intent.putExtra("url",a.getUrl());
        context.startActivity(intent);
           }
       });

        if (news.favoriteDatabase.favoriteDao().isFavorite(articles.get(position).getId())==1)
        holder.bookmark.setImageResource(R.drawable.ic_bookmark);
        else
            holder.bookmark.setImageResource(R.drawable.ic_baseline_bookmark_border_24);


        holder.bookmark.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FavoriteList favoriteList = new FavoriteList();
                int id = articles.get(position).getId();
                String source = articles.get(position).getSource().getName();
                String author = articles.get(position).getAuthor();
                String publishedAt = articles.get(position).getPublishedAt();
                String description = articles.get(position).getDescription();
                String title = articles.get(position).getTitle();
                String url = articles.get(position).getUrl();
                String urlToImage = articles.get(position).getUrlToImage();
                 favoriteList.setId(id);
                favoriteList.setAuthor(author);
                favoriteList.setDescription(description);
                favoriteList.setSource(source);
                favoriteList.setPublishedAt(publishedAt);
                favoriteList.setTitle(title);
                favoriteList.setUrl(url);
                favoriteList.setUrlToImage(urlToImage);
                favoriteList.setPublishedAt(dateTime(articles.get(position).getPublishedAt()));

                if (news.favoriteDatabase.favoriteDao().isFavorite(id)!=1){
                    holder.bookmark.setImageResource(R.drawable.ic_bookmark);
                    news.favoriteDatabase.favoriteDao().addData(favoriteList);

                }else {    
                    holder.bookmark.setImageResource(R.drawable.ic_baseline_bookmark_border_24);
                    news.favoriteDatabase.favoriteDao().delete(favoriteList);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return articles.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvTitle, tvSource, tvDate;
        ImageView imageView;
        ImageButton bookmark;
        CardView cardView;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvTitle = itemView.findViewById(R.id.tvId);
            tvSource = itemView.findViewById(R.id.tvSource);
            tvDate = itemView.findViewById(R.id.tvDate);
            imageView = itemView.findViewById(R.id.image);
            cardView = itemView.findViewById(R.id.cardView);
            bookmark = itemView.findViewById(R.id.favrr);
        }
    }

调试步骤:

  1. 收藏 2-3 件商品。
  2. 重新启动应用程序。 检查它是否在重新启动应用程序后将这些项目显示为收藏。

还将日志添加到您更改可绘制对象的条件中。

在查看您的 JSON 后,id 似乎是造成问题的原因。您的所有 json 项目的 ID 为空,因此当收藏时。一个它显示最喜欢的。给大家。

解决方案:使用另一个字段检查数据是否添加到 fav.list

删除也不行 尝试

@Query("DELETE FROM favoritelist WHERE title = :title")
void deleteByUserId(String title);

要删除项目

还有https://github.com/amitshekhariitbhu/Android-Debug-Database

检查这个库来调试你的数据库