gpt4 book ai didi

android - 如何在 android fragment 中使用 SQLite?

转载 作者:行者123 更新时间:2023-11-29 18:29:33 24 4
gpt4 key购买 nike

我是一名有抱负的 Android 开发者。我想做一个支持 SQLtite 的简单项目,但我遇到了 SQLite 和 Fragment 的问题。

预先感谢您的帮助

我尝试将 context: FragmentTwo 替换为 context: MainActivity

结果又是一个错误。

据此,我得出结论,这不是解决问题的方法。😕😕😕

class sql(context: FragmentTwo):SQLiteOpenHelper(context, DATABASE_NAME, null, DATABASE_VER) {
companion object{
private val DATABASE_VER=1
private val DATABASE_NAME="GUESTS.db"

private val TABLE_NAME= "GUESTS"
private val COL_ID= "Id"
private val COL_NAME= "Name"
private val COL_CITY= "City"

}
override fun onCreate(db: SQLiteDatabase?) {
val CREATE_TABLE_QUERY = ("CREATE TABLE $TABLE_NAME +($COL_ID INTEGER PRIMARY KEY,$COL_NAME TEXT, $COL_CITY TEXT)")
db!!.execSQL(CREATE_TABLE_QUERY)

}

override fun onUpgrade(db: SQLiteDatabase?, p1: Int, p2: Int) {
db!!.execSQL("DROP TABLE IF EXISTS $TABLE_NAME")
onCreate(db!!)
}
val allguest:List<guest>
get(){
val listguest = ArrayList<guest>()
val selectQuery = "SELECT * FROM $TABLE_NAME"
val db = this.writableDatabase
val cursor = db.rawQuery(selectQuery, null)
if (cursor.moveToFirst()){
do {
val guest= guest()
guest.id=cursor.getInt(cursor.getColumnIndex(COL_ID))
guest.name=cursor.getString(cursor.getColumnIndex(COL_NAME))
guest.city=cursor.getString(cursor.getColumnIndex(COL_CITY))

listguest.add(guest)

}
while (cursor.moveToNext())
}
db.close()
return listguest
}
fun addGuest(guest: guest){

val db = this.writableDatabase
val values = ContentValues()
values.put(COL_ID, guest.id)
values.put(COL_NAME, guest.name)
values.put(COL_CITY, guest.city)

db.insert(TABLE_NAME, null, values)
db.close()
}

fun updateGuest(guest: guest):Int{

val db = this.writableDatabase
val values = ContentValues()
values.put(COL_ID, guest.id)
values.put(COL_NAME, guest.name)
values.put(COL_CITY, guest.city)

return db.update(TABLE_NAME, values, "$COL_ID=?", arrayOf(guest.id.toString()))

}

fun deleteGuest(guest: guest){

val db = this.writableDatabase



db.delete(TABLE_NAME, "$COL_ID=?", arrayOf(guest.id.toString()))
db.close()

}
}

不能使用提供的参数调用以下函数:

public constructor SQLiteOpenHelper(@RecentlyNullable p0: Context!, @RecentlyNullable p1: String!, @RecentlyNullable p2: ((SQLiteDatabase!, SQLiteCursorDriver!, String!, SQLiteQuery!) -> Cursor!)!, p3: Int) defined in android.database.sqlite.SQLiteOpenHelper
public constructor SQLiteOpenHelper(@RecentlyNullable p0: Context!, @RecentlyNullable p1: String!, @RecentlyNullable p2: SQLiteDatabase.CursorFactory!, p3: Int) defined in android.database.sqlite.SQLiteOpenHelper
public constructor SQLiteOpenHelper(@RecentlyNullable p0: Context!, @RecentlyNullable p1: String!, p2: Int, @RecentlyNonNull p3: SQLiteDatabase.OpenParams!) defined in android.database.sqlite.SQLiteOpenHelper

最佳答案

SQLiteOpenHelper 构造函数的第一个参数是 ContextFragment 不是 Context

战术上,替换:

class sql(context: FragmentTwo):SQLiteOpenHelper(context, DATABASE_NAME, null, DATABASE_VER) {

与:

class sql(fragment: FragmentTwo):SQLiteOpenHelper(fragment.requireContext(), DATABASE_NAME, null, DATABASE_VER) {

但是,在现代 Android 应用程序开发中, fragment 不应直接与 SQLiteOpenHelper 一起使用。至少,该 fragment 应该与 ViewModel 一起工作,而后者又与单例 SQLiteOpenHelper 一起工作。确保您用于学习 Android 应用程序开发的任何书籍或类(class)都向您展示了如何使用 Jetpack/AndroidX 库,包括 ViewModelLiveData 等内容。

关于android - 如何在 android fragment 中使用 SQLite?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56973588/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com