因此,我在单击 Fragment QuestionFragment.kt 中的图像视图时存储两个文本视图,并使用 safeargs 将其发送到另一个片段 (ToLearnFragment.kt)。因此,当我单击将把这个片段带到我这里的按钮时,我收到一个错误消息,提示无法找到 Room Database 的实现。下面是我写的代码。
MyApp.kt
@HiltAndroidApp
class MyApp:Application() {
// No need to cancel this scope as it'll be torn down with the process
val applicationScope = CoroutineScope(SupervisorJob())
// Using by lazy so the database and the repository are only created when they're needed
// rather than when the application starts
val database by lazy { FlashCardPairDatabase.getDatabase(this,applicationScope) }
val repository by lazy { FlashcardRepository(database.flashCardPairDao()) }
}
ToLearnFragment.kt
class ToLearnFragment : Fragment() {
private lateinit var binding: FragmentToLearnBinding
private lateinit var flashcardPairViewmodel: FlashcardPairViewmodel
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
binding = FragmentToLearnBinding.inflate(inflater,container,false)
val adapter = FlashcardListAdapter()
binding.recyclerView.adapter = adapter
binding.recyclerView.layoutManager = LinearLayoutManager(context)
flashcardPairViewmodel = ViewModelProvider(this,FlashcardViewModelFactory((requireActivity().application as MyApp).repository)).get(FlashcardPairViewmodel::class.java)
flashcardPairViewmodel.allflashcardPairs.observe(viewLifecycleOwner, Observer { flashCardPair ->
flashCardPair?.let {
adapter.submitList(it)
}
})
// Get the arguments passed from the previous fragment
val args = arguments
// Get the values from the arguments
val key = args?.getString("key")
val value = args?.getString("value")
// Use the key and value as needed
Log.d("ToLearnFragment", "Key: $key, Value: $value")
return binding.root
}
}
我创建的房间数据库如下 抽认卡对.kt
@Entity("flash_pair")
data class FlashCardPair(
@PrimaryKey @ColumnInfo(name = "front") val front: String,
@ColumnInfo(name = "back") val back: String
)
FlashCardpairDao.kt
@Dao
interface FlashCardPairDao {
@Query("SELECT * FROM flash_pair ORDER BY FlashCardPair ASC")
fun getFlashcardPairs(): Flow<List<FlashCardPair>>
@Insert(onConflict = OnConflictStrategy.IGNORE)
fun insertFlashCardPair(flashCardPair: FlashCardPair)
@Query("DELETE FROM flash_pair")
fun deleteAll()
}
FlashCardPairDatabase.kt
@Database(entities = arrayOf(FlashCardPair::class), version = 1, exportSchema = false)
abstract class FlashCardPairDatabase :RoomDatabase(){
abstract fun flashCardPairDao():FlashCardPairDao
private class FlashcardCallback(
private val scope: CoroutineScope
):Callback(){
override fun onCreate(db: SupportSQLiteDatabase) {
super.onCreate(db)
INSTANCE?.let {
scope.launch {
val flashcardao = it.flashCardPairDao()
var flashCardPair = FlashCardPair("hello","Back")
flashcardao.insertFlashCardPair(flashCardPair)
}
}
}
}
companion object {
// Singleton prevents multiple instances of database opening at the
// same time.
@Volatile
private var INSTANCE: FlashCardPairDatabase? = null
fun getDatabase(context: Context,
scope: CoroutineScope
):FlashCardPairDatabase {
// if the INSTANCE is not null, then return it,
// if it is, then create the database
return INSTANCE ?: synchronized(this) {
val instance = Room.databaseBuilder(
context.applicationContext,
FlashCardPairDatabase::class.java,
"flashpair_database",
).build()
INSTANCE = instance
// return instance
instance
}
}
}
}
帮助我保存数据的代码如下(在QuestionsFragment.kt中)
// onclick listener on the image
binding.imageFlashCard.setOnClickListener {
// ViewModel for counting
counterviewModel.addWordCount()
binding.textCounter.text = counterviewModel.count.toString()
// increment currentPairIndex and get the next pair
currentPairIndex++
if (currentPairIndex >= hashMap.size) {
// if we have reached the end of the hashmap, start again from the beginning
currentPairIndex = 0
}
currentPair = hashMap.entries.elementAt(currentPairIndex)
// update the UI with the new pair
binding.textCardFront.text = currentPair.key
binding.textCardBack.text = hashMap[currentPair.key]
//passing key and value pair to TolearnFragment.kt
val bundle = Bundle()
bundle.putString("key",currentPair.key)
bundle.putString("value",hashMap[currentPair.key])
}
下面是我的 logcat,它显示了正在发生的事情的错误。
FATAL EXCEPTION: main
Process: com.example.visuallithuanian, PID: 3420
java.lang.RuntimeException: Cannot find implementation for com.example.visuallithuanian.database.FlashCardPairDatabase. FlashCardPairDatabase_Impl does not exist
at androidx.room.Room.getGeneratedImplementation(Room.kt:58)
at androidx.room.RoomDatabase$Builder.build(RoomDatabase.kt:1347)
at com.example.visuallithuanian.database.FlashCardPairDatabase$Companion.getDatabase(FlashCardPairDatabase.kt:53)
at com.example.visuallithuanian.MyApp$database$2.invoke(MyApp.kt:19)
at com.example.visuallithuanian.MyApp$database$2.invoke(MyApp.kt:19)
at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)
at com.example.visuallithuanian.MyApp.getDatabase(MyApp.kt:19)
at com.example.visuallithuanian.MyApp$repository$2.invoke(MyApp.kt:20)
at com.example.visuallithuanian.MyApp$repository$2.invoke(MyApp.kt:20)
at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)
at com.example.visuallithuanian.MyApp.getRepository(MyApp.kt:20)
at com.example.visuallithuanian.ToLearnFragment.onCreateView(ToLearnFragment.kt:34)
at androidx.fragment.app.Fragment.performCreateView(Fragment.java:3104)
at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:524)
at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:261)
at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1890)
at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1814)
at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1751)
at androidx.fragment.app.FragmentManager$5.run(FragmentManager.java:538)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loopOnce(Looper.java:201)
at android.os.Looper.loop(Looper.java:288)
at android.app.ActivityThread.main(ActivityThread.java:7870)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)