Android studio 在kotlin中关闭应用程序时,如何使用room database deleteAll()?
我已经为historyactivity设置了roomdatabase,并且可以使用insert方法。 现在,当我单击我的historyactivity时,数据仍然显示为RecyclerViewItems。 但我想在关闭构建应用程序时重置数据。我怎么做?有人能帮忙吗? 在我的DAO中,你可以看到所有的乐趣,所以当我关闭我的应用程序时,我如何使用它?? 历史.ktAndroid studio 在kotlin中关闭应用程序时,如何使用room database deleteAll()?,android-studio,kotlin,android-room,Android Studio,Kotlin,Android Room,我已经为historyactivity设置了roomdatabase,并且可以使用insert方法。 现在,当我单击我的historyactivity时,数据仍然显示为RecyclerViewItems。 但我想在关闭构建应用程序时重置数据。我怎么做?有人能帮忙吗? 在我的DAO中,你可以看到所有的乐趣,所以当我关闭我的应用程序时,我如何使用它?? 历史.kt @Entity(tableName = "history") class History(@PrimaryKey(autoGenera
@Entity(tableName = "history")
class History(@PrimaryKey(autoGenerate = true) var id: Long?,
@ColumnInfo(name= "item") var item:String?,
@ColumnInfo(name = "title") var title:String?,
@ColumnInfo(name = "price") var price:Int?,
@ColumnInfo(name = "Image") var Image:String?
) {
constructor(): this(null,"","",0,"")
}
@Dao
interface HistoryDao {
@Query("SELECT * FROM history")
fun getAll(): List<History>
@Insert(onConflict = REPLACE )
fun insert(history: History)
@Query("DELETE from history")
fun deleteAll(): List<History>
}
历史活动
class HistoryActivity : AppCompatActivity(), SwipeRefreshLayout.OnRefreshListener {
private var historyDb: HistoryDB? = null
private var historyList = listOf<History>()
lateinit var adapter: HistoryitemReclycerViewAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_history)
historyDb = HistoryDB.getInstance(this)
adapter = HistoryitemReclycerViewAdapter(this, historyList)
val r = Runnable {
try {
historyList = historyDb?.HistoryDB()?.getAll()!!
adapter = HistoryitemReclycerViewAdapter(this, historyList)
adapter.notifyDataSetChanged()
swipeRefreshLo.setOnRefreshListener(this)
recyclerview.adapter = adapter
recyclerview.layoutManager = LinearLayoutManager(this)
} catch (e: Exception) {
}
}
val thread = Thread(r)
thread.start()
}
override fun onRefresh() {
swipeRefreshLo.isRefreshing = false
}
override fun onDestroy() {
HistoryDB.destoryInstance()
historyDb =null
super.onDestroy()
}
}
似乎您需要一个内存中的数据库。您可以使用而不是Room.databaseBuilder。这是此生成器的文档: 为内存中的数据库创建RoomDatabase.Builder。存储在内存中的信息 进程终止时,数据库将消失。 一旦建立了数据库,就应该保留对它的引用并重新使用它
你核对我的答案了吗?如果它对你有用,请接受它
class HistoryActivity : AppCompatActivity(), SwipeRefreshLayout.OnRefreshListener {
private var historyDb: HistoryDB? = null
private var historyList = listOf<History>()
lateinit var adapter: HistoryitemReclycerViewAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_history)
historyDb = HistoryDB.getInstance(this)
adapter = HistoryitemReclycerViewAdapter(this, historyList)
val r = Runnable {
try {
historyList = historyDb?.HistoryDB()?.getAll()!!
adapter = HistoryitemReclycerViewAdapter(this, historyList)
adapter.notifyDataSetChanged()
swipeRefreshLo.setOnRefreshListener(this)
recyclerview.adapter = adapter
recyclerview.layoutManager = LinearLayoutManager(this)
} catch (e: Exception) {
}
}
val thread = Thread(r)
thread.start()
}
override fun onRefresh() {
swipeRefreshLo.isRefreshing = false
}
override fun onDestroy() {
HistoryDB.destoryInstance()
historyDb =null
super.onDestroy()
}
}