这是我的AndroidManifest.html文件的片段:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/Theme.Androidstudio"
        tools:targetApi="31">
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

我已经判断了我项目的styles.html文件,以确保"@style/Theme.Androidstudio"定义正确,并且它确实存在.然而,Android Studio似乎无法解决这个符号.

我try 过清理和重建项目、同步Gradle文件,甚至重新启动Android Studio,但错误仍然存在.

除了解决此错误外,我还寻求有关在Kotlin Android应用程序中实施CRUD操作的指导.非常感谢您提供与Kotlin中的CRUD操作相关的任何建议、资源或示例.

推荐答案

请参阅下面的解决方案,我还提供了在Kotlin中实现CRUD操作的分步过程:

使用SQITE进行CRUD操作

转到AndroidManifest.html

添加这些权限-

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />

标签下方和标签上方

喜欢这里

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/Theme.Androidstudio"
        tools:targetApi="31">
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

}

class MainActivity : AppCompatActivity() {

添加此行

private lateinit var db:SQLiteDatabase

setContentView(R.layout.activity_main)

添加此行

 db = openOrCreateDatabase("employeemanager.db", MODE_PRIVATE,null)

创建表

val createTableQuery = "CREATE TABLE IF NOT EXISTS employee(employeeid INT PRIMARY KEY, name TEXT, phonenumber INT, country TEXT)"
        db?.execSQL(createTableQuery)

插入表格

 val insertQuery = "INSERT INTO employee VALUES("+idtext+",'"+nametext+"',"+phonenumber+",'"+country+"')"
                db?.execSQL(insertQuery)
                Toast.makeText(this@MainActivity, "Inserted Data Successfully!",Toast.LENGTH_SHORT).show()

将提取的数据添加到旋转器

val fetchdata = "SELECT DISTINCT country FROM employee"
        val cursor = db?.rawQuery(fetchdata,null)
        val countries = ArrayList<String>()
        while(cursor!!.moveToNext()) {
            val country1 = cursor.getString(0)
            countries.add(country1)
        }
        cursor?.close()
        var spinner = findViewById<Spinner>(R.id.spinner)
        val spinneradapter = ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item,countries)
        spinneradapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        spinner.adapter = spinneradapter
        spinner.onItemSelectedListener = object : OnItemSelectedListener {
            override fun onItemSelected(parent: AdapterView<*>, view: View, position: Int, id: Long) {
                selectedItem = parent.getItemAtPosition(position).toString();
                Toast.makeText(this@SecondActivity, "$selectedItem",Toast.LENGTH_SHORT).show()

            }
            override fun onNothingSelected(parent: AdapterView<*>) {
            }
        }

Select 数据并显示

 search.setOnClickListener {
            val searchcountrydata = "SELECT * FROM employee WHERE country =" +"'$selectedItem'";
            val cursor2 = db?.rawQuery(searchcountrydata,null)
            val displayString = StringBuilder()
            while(cursor2!!.moveToNext()) {
                val id = cursor2.getString(0)
                val name = cursor2.getString(1)
                val phonenumber = cursor2.getString(2)
                val country = cursor2.getString(3)
                displayString.append("ID: $id, Name: $name, PhoneNumber: $phonenumber, Country: $country" +"\n")
            }
            display2.text = displayString.toString()
        }

排序数据

sort.setOnClickListener {
            val newquery = "SELECT * FROM employee ORDER BY employeeid DESC"
            val cursor3 = db?.rawQuery(newquery,null)
            val displayString2 = StringBuilder()
            while(cursor3!!.moveToNext()) {
                val id = cursor3.getString(0)
                val name = cursor3.getString(1)
                val phonenumber = cursor3.getString(2)
                val country = cursor3.getString(3)
                displayString2.append("ID: $id, Name: $name, PhoneNumber: $phonenumber, Country: $country" +"\n")
            }
            display2.text = displayString2.toString()
        }

更新数据

updatebutton.setOnClickListener {
            val empval = empid.text.toString()
            val countryval = country.text.toString()
            val updatequery = "UPDATE employee SET country =" + "'$countryval'"+" WHERE employeeid = $empval"
            db?.execSQL(updatequery)
            Toast.makeText(this@ThirdActivity, "Updated Successfully",Toast.LENGTH_SHORT).show()

        }

删除日期

delete.setOnClickListener {
        val rollval = rollno.text.toString();
        val deletequery = "DELETE FROM STUDENT WHERE ROLLNO=$rollval";
        db?.execSQL(deletequery)
        Toast.makeText(this@MainActivity,"Deleted Data Successfully!",Toast.LENGTH_SHORT).show()
    }

在数据中找到最大值和最小值

   maxandmin.setOnClickListener {
 val selectquery1  = "SELECT productid, name, price FROM PRODUCTS p WHERE p.price IN (SELECT MAX(price) FROM PRODUCTS)"
        val selectquery2 = "SELECT productid, name, price FROM PRODUCTS p WHERE p.price IN (SELECT MIN(price) FROM PRODUCTS)"
        val cursor1 = db?.rawQuery(selectquery1, null)
        val cursor2 = db?.rawQuery(selectquery2, null)
        val str = StringBuilder()
        str.append("Maximum Priced Product Details: \n")
        while(cursor1!!.moveToNext()) {
            val id = cursor1.getString(0)
            val name = cursor1.getString(1)
            val price = cursor1.getString(2)
            str.append("Product ID: $id, Product Name: $name, Product Price: $price")
        }
        str.append("\n\nMinimum Priced Product Details: \n")
        while(cursor2!!.moveToNext()) {
            val id = cursor2.getString(0)
            val name = cursor2.getString(1)
            val price = cursor2.getString(2)
            str.append("Product ID: $id, Product Name: $name, Product Price: $price")
        }

查找总计数

str.append("\n\nTotal No Of Products: ")
            val findtotproducts = "SELECT COUNT(*) FROM PRODUCTS"
            val totprice = "SELECT SUM(PRICE) FROM PRODUCTS"
            val cursor4 = db?.rawQuery(findtotproducts,null)
            val cursor5 = db?.rawQuery(totprice, null)

            while(cursor4!!.moveToNext()) {
                val countprod = cursor4.getString(0)
                str.append(countprod)
            }

查找总价

     str.append("\n\nTotal Price of Products: ")
        while(cursor5!!.moveToNext()) {
            val countprod2 = cursor5.getString(0)
            str.append(countprod2)
        }
        display.text = str;

Android相关问答推荐

打开平板电脑的下载文件夹中的文件,例如使用其mimeType将Intent发送到我们的应用程序

如何从sqlite数据库中检索数据到碎片android?

从片段导航回来

没有打开历史记录的活动意向 Select 器完成调用活动

activity在 Runnable 中如何工作?我的 Android 表格未显示

可从 Play store 下载链接访问未发布的应用

单击按钮时不显示 Toast 消息

如何知道我的应用程序的新版本是否显示广告?

判断 AAR 元数据时发现 Android 问题:androidx.core:core:1.12.0-alpha01 和 androidx.core:core-ktx:1.12.0-alpha01

在 Kotlin 中设置 startActivity() 时类型不匹配

如何在 Jetpack Compose 中设置行宽等于 TextField 的宽度?

在事件中使用 Context/Toast 时不需要的重组 - Jetpack Compose

为什么项目捕获对象给我在 Compose 中找不到参考

Jetpack compose 未解决的参考错误

如何在 compose 中使用 BottomSheetScaffold 为底页设置半展开高度?

是什么让 Android Studio 中的按钮变成紫色?加上新手的其他奇怪行为

Android 12 通过包管理器中断 APK 安装?

Kotlin Coroutines Dispatchers.IO 没有创建预期的线程

无法使用 Gradle 托管设备对基线配置文件运行测试

compose FontFamily 错误:必须初始化变量