我试图创建一个简单的应用程序,用户可以在其中输入文本到编辑文本字段.按下按钮后,输入内容就会保存到SQLite数据库中.
我遵循了以下链接中的指南(并try 适应我的应用程序):
https://www.geeksforgeeks.org/android-sqlite-database-in-kotlin/个
然而,当在Android Studio中运行应用程序时,我得到错误:
Type mismatch: inferred type is AddRecipeFragment but Context was expected
我的AddRecipeFragment.kt写道:
package com.example.recipy
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import com.example.recipy.databinding.FragmentAddRecipeBinding
class AddRecipeFragment : Fragment() {
private var _binding: FragmentAddRecipeBinding? = null
// This property is only valid between onCreateView and
// onDestroyView.
private val binding get() = _binding!!
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
_binding = FragmentAddRecipeBinding.inflate(inflater, container, false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
// val saveRecipeButton = findViewById(R.id.save_recipe_button) as Button
binding.saveRecipeButton.setOnClickListener{
val db = DBHelper(this, null)
val name = binding.recipeNameTextInput.text.toString()
db.addRecipe(name)
Toast.makeText(this, name + " added to database", Toast.LENGTH_LONG).show()
binding.recipeNameTextInput.text.clear()
}
}
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
}
我的fragment_add_recipe.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".AddRecipeFragment">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">
<EditText
android:id="@+id/recipe_name_text_input"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:inputType="text"
app:layout_constraintTop_toTopOf="parent"/>
<Button
android:id="@+id/save_recipe_button"
android:layout_width="150sp"
android:layout_gravity="center"
android:text="Save"
android:textColor="#ffffff"
android:textSize="20sp"
android:layout_height="wrap_content"
android:layout_marginTop="20sp"
app:layout_constraintTop_toBottomOf="@id/Name"/>
<TextView
android:id="@+id/Name"
android:textAlignment="center"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20sp"
android:text="Name\n\n"
android:textSize="22sp"
android:padding="10sp"
android:textColor="#000000"
app:layout_constraintTop_toBottomOf="@id/recipe_name_text_input"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
和DBHelper.kt:
package com.example.recipy
import android.content.ContentValues
import android.content.Context
import android.database.Cursor
import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteOpenHelper
public class DBHelper(context: Context, factory: SQLiteDatabase.CursorFactory?) :
SQLiteOpenHelper(context, DATABASE_NAME, factory, DATABASE_VERSION) {
// below is the method for creating a database by a sqlite query
override fun onCreate(db: SQLiteDatabase) {
val query = ("CREATE TABLE " + TABLE_NAME + " ("
+ ID_COL + " INTEGER PRIMARY KEY, " +
NAME_COl + " TEXT," + ")")
db.execSQL(query)
}
override fun onUpgrade(db: SQLiteDatabase, p1: Int, p2: Int) {
// this method is to check if table already exists
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME)
onCreate(db)
}
// This method is for adding data in our database
fun addRecipe(name : String){
val values = ContentValues()
values.put(NAME_COl, name)
val db = this.writableDatabase
db.insert(TABLE_NAME, null, values)
db.close()
}
companion object{
// here we have defined variables for our database
// below is variable for database name
private val DATABASE_NAME = "RECIPY_DB"
// below is the variable for database version
private val DATABASE_VERSION = 1
// below is the variable for table name
val TABLE_NAME = "recipes_table"
// below is the variable for id column
val ID_COL = "id"
// below is the variable for name column
val NAME_COl = "name"
}
}