我有桌面网站和收藏夹。我想将网站添加到收藏夹,当我点击recyclerView中的checkBox并从收藏夹中删除时,我第二次点击复选框。 我认为这应该是多对多的关系。我创建了以下表格:
网站已
@Entity
public class WebSites {
@PrimaryKey(autoGenerate = true)
public int id;
@ColumnInfo()
public String website_head;
public WebSites(@NonNull String website_head {
this.website_head = website_head;
}
收藏夹
@Entity
public class Favorites {
@PrimaryKey(autoGenerate = true)
private int id;
}
FavoritesWithWebSites
@Entity(tableName = "favorites_with_websites",
primaryKeys = { "websitesId", "favId"},
foreignKeys = {
@ForeignKey(entity = WebSites.class,
parentColumns = "id",
childColumns = "websitesId"),
@ForeignKey(entity = Favorites.class,
parentColumns = "id",
childColumns = "favId")
})
public class FavoritesWithWebSites {
public final int websitesId;
public final int favId;
public FavoritesWithWebSites(int websitesId, int favId) {
this.websitesId = websitesId;
this.favId = favId;
}
}
来自道的询问:
@Query("SELECT * FROM websites INNER JOIN favorites_with_websites ON " +
"websites.id = favorites_with_websites.websitesId")
LiveData<List<WebSites>> finGet();
我不明白在点击chkebox时我必须如何添加到收藏夹并从收藏夹中删除。当我添加数据时,我不明白我必须向FavoritesWithWebSites添加哪些参数。
我的适配器
public class MyRecViewAdapter extends
RecyclerView.Adapter<MyRecViewAdapter.ViewHolder> {
private final LayoutInflater mInflater;
private List<WebSites> mWebSites; // Cached copy of words
private Context context;
private WebSitesViewModel webSitesViewModel;
class ViewHolder extends RecyclerView.ViewHolder {
private final TextView tvHead;
private final TextView tvDesc;
private final ImageView ivWebImage;
private final CheckBox chbFavorites;
private ViewHolder(View itemView) {
super(itemView);
tvHead = itemView.findViewById(R.id.tv_head);
tvDesc = itemView.findViewById(R.id.tv_desc);
ivWebImage = itemView.findViewById(R.id.ivWebImage);
chbFavorites = itemView.findViewById(R.id.chbFavorites);
}
}
public List<WebSites> getmWebSites() {
return mWebSites;
}
public MyRecViewAdapter(Context context, WebSitesViewModel
webSitesViewModel) {
this.context = context;
mInflater = LayoutInflater.from(this.context);
this.webSitesViewModel = webSitesViewModel;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = mInflater.inflate(R.layout.list_item, parent, false);
return new ViewHolder(itemView);
}
@Override
public void onBindViewHolder(final ViewHolder holder, final int position)
{
if (mWebSites != null) {
final WebSites current = mWebSites.get(position);
holder.tvHead.setText(current.getWebsite_head());
holder.tvDesc.setText(current.getWebsite_url());
holder.chbFavorites.setChecked(current.getSelected());
holder.chbFavorites.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (holder.chbFavorites.isChecked()) {
//????????????????????
}
else {
//??????????????
}
}
});
Picasso.with(context).load(current.getImg_src())
.error(R.drawable.ic_launcher_foreground)
.placeholder(R.drawable.ic_launcher_foreground)
.into(holder.ivWebImage);
} else {
// Covers the case of data not being ready yet.
holder.tvHead.setText("No Word");
holder.tvDesc.setText("No Word");
}
}
public void setWebSites(List<WebSites> webSites){
mWebSites = webSites;
notifyDataSetChanged();
}
// getItemCount() is called many times, and when it is first called,
// mWords has not been updated (means initially, it's null, and we can't
return null).
@Override
public int getItemCount() {
if (mWebSites != null)
return mWebSites.size();
else return 0;
}
}
答案 0 :(得分:0)
这不是M:M的关系。
您可以在网站中添加此字段:
@ColumnInfo(name = "is_fav")
private boolean isFavorite;
在你的适配器中使用它:
holder.chbFavorites.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean isFav = holder.chbFavorites.isChecked();
current.setIsFavorite(isFav);
yourWebsitesDao.update(current);
}
});