I am following this tutorial from Android SQLite Database in Kotlin - GeeksforGeeks <<
The tutorial is outdated, so I had to make a few changes to the code.
The idea is to create a simple SQLite DB and use it to store and retrieve data.
The MainActivity.kt code that leads to an error is given below:
class MainActivity : AppCompatActivity(){
@SuppressLint("Range")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
findViewById<Button>(R.id.addData).setOnClickListener {
val db = DBHelper(this, null)
val name = findViewById<EditText>(R.id.enterName).text.toString()
val age = findViewById<EditText>(R.id.enterAge).text.toString()
db.addName(name, age)
Toast.makeText(this, name + "added to database ", +Toast.LENGTH_LONG).show()
findViewById<EditText>(R.id.enterName).text.clear()
findViewById<EditText>(R.id.enterAge).text.clear()
}
val printD = findViewById<Button>(R.id.printData) as Button
printD.setOnClickListener{
val db = DBHelper(this,null)
val cursor = db.getName()
val nameList = findViewById<TextView>(R.id.nameList) as TextView
val ageList = findViewById<TextView>(R.id.ageList) as TextView
cursor!!.moveToFirst()
nameList.append(cursor.getString(cursor.getColumnIndex(DBHelper.NAME_COL))+"\n")
ageList.append(cursor.getString(cursor.getColumnIndex(DBHelper.AGE_COL))+"\n")
while(cursor.moveToNext())
{
nameList.append(cursor.getString(cursor.getColumnIndex(DBHelper.NAME_COL)) + "\n")
ageList.append(cursor.getString(cursor.getColumnIndex(DBHelper.AGE_COL)) + "\n")
}
}
}
The part which actually throws the error is given below:
fun addName(name: String, age: String){
val values = ContentValues()
values.put(NAME_COL, name)
values.put(AGE_COL, age)
val db = this.writableDatabase
return db.rawQuery("SELECT * from "+ TABLE_NAME, Unit)
}
The error I get are:
Type mismatch: inferred type is Cursor! but Unit was expected
and
Type mismatch: inferred type is Unit but Array<(out) String!>! was expected
This is my first time working with Android … so I am at a loss to understand where things are going wrong.
Any help is appreciated.
Thanks.