最近我开始在 kotlin 中学习 Room 数据库,我在主线程中进行所有操作以进行测试,现在我开始使用视图模型,但我不知道如何在 recyclerview 适配器中使用视图模型来执行一些操作,例如检查是否一个值是否存在于数据库中。我的问题是我如何在 recyclerview 适配器中访问房间数据库 dao 以检查一个项目是否是最喜欢的,然后单击按钮从数据库中添加或删除该项目这是我正在尝试学习的资源。
这是我的代码
适配器
class QuotesAdapter(private val context: Context,
private val quoteList: List<QuotesByCategory>,
private val itemClickListener: (Int) -> Unit):
RecyclerView.Adapter<QuotesAdapter.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.quotes_list_item, parent, false)
return ViewHolder(view)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val dataList = quoteList[position]
val currentQuote = dataList.quotes[position]
val currentCategory = dataList.category
val quote = currentQuote.quote
holder.quoteText.text = quote
holder.itemView.setOnClickListener { itemClickListener.invoke(position) }
// This part is what i'm trying to do with view model
val favouritesDao = AppDatabase.getDatabase(context).quoteDao()
if (favouritesDao.isFavourite(currentQuote.id)) {
holder.favBtn.setImageResource(R.drawable.ic_favorite)
} else {
holder.favBtn.setImageResource(R.drawable.ic_favorite_border)
}
holder.favBtn.setOnClickListener {
if (favouritesDao.isFavourite(currentQuote.id)) {
favouritesDao.deleteFavouriteById(currentQuote.id)
holder.favBtn.setImageResource(R.drawable.ic_favorite_border)
Toast.makeText(context, "Favourite Deleted", Toast.LENGTH_SHORT).show()
} else {
favouritesDao.addFavourite(
Favourite(currentQuote.id,quote,currentCategory.categoryName)
)
holder.favBtn.setImageResource(R.drawable.ic_favorite)
Toast.makeText(context, "Favourite Added", Toast.LENGTH_SHORT).show()
}
}
}
这是我的片段
class QuotesFragment : Fragment() {
private val viewModel: QuoteViewModel by activityViewModels {
QuoteViewModelFactory(
(activity?.application as QuotesApplication).database.quoteDao()
)
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_quotes, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
val categoryId = arguments?.getInt(Constants.STRING_EXTRA_CAT_ID, 0)
val categoryName = arguments?.getString(Constants.STRING_EXTRA_CAT_NAME )
// Get a reference to the activity's ActionBar
val actionBar = (activity as AppCompatActivity).supportActionBar
actionBar?.setDisplayHomeAsUpEnabled(true)
actionBar?.title = categoryName
val rvQuotes: RecyclerView = view.findViewById(R.id.quotes_recyclerview)
rvQuotes.layoutManager = LinearLayoutManager(requireContext())
lifecycle.coroutineScope.launch {
viewModel.getQuotesByCategory(categoryId!!).collect {
val quotesAdapter = QuotesAdapter(
requireContext(), quoteList = it,
itemClickListener = { position ->
val bundle = bundleOf(
Constants.STRING_EXTRA_CAT_ID to categoryId,
Constants.STRING_EXTRA_ADAPTER_POSITION to position
)
findNavController().navigate(R.id.action_quotesFragment_to_quoteDetailFragment, bundle)
}
)
rvQuotes.adapter = quotesAdapter
}
}
}
}
任何帮助将不胜感激。
val favouritesDao = AppDatabase.getDatabase(context).quoteDao()
if (favouritesDao.isFavourite(currentQuote.id)) {
holder.favBtn.setImageResource(R.drawable.ic_favorite)
} else {
holder.favBtn.setImageResource(R.drawable.ic_favorite_border)
}
这个代码的价格让我的应用程序崩溃,说房间不能在主线程上运行。 所以我的问题是如何在 recyclerview 适配器中访问视图模型。