马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
继续修改昨天的项目,将activity_main.xml文件修改为:<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context="com.example.xinwei.databasekotlin.MainActivity">
<Button
android:id="@+id/idbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="创建数据库并插入数据"/>
<EditText
android:id="@+id/idinputtext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入要查询的人"/>
<Button
android:id="@+id/idquerybutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="读取数据"/>
<TextView
android:id="@+id/idreadtext"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
将MainActivity.kt文件修改为:package com.example.xinwei.databasekotlin
import android.content.ContentValues
import android.database.Cursor
import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteQueryBuilder
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
var myDatabase:SQLiteDatabase?=null
var contentValues=ContentValues()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val myDatabaseHelper=CreateDatabase(this,"myDatabase",null,1)
myDatabase=myDatabaseHelper.writableDatabase
idbutton.setOnClickListener {
dummyDataArray.add(MyData("jerryxjr1220","30","男"))
dummyDataArray.add(MyData("wei_Y","18","男"))
dummyDataArray.add(MyData("alltolove","37","男"))
insertValues(0)
insertValues(1)
insertValues(2)
}
idquerybutton.setOnClickListener{
var inputText=idinputtext.text
var projection= arrayOf("id","name","age","gent")
var curser=queryData(projection,"name like ?", arrayOf("%${inputText}%"),"name")
if (curser.moveToFirst()){
do {
var id=curser.getInt(curser.getColumnIndex("id"))
var name=curser.getString(curser.getColumnIndex("name"))
var age=curser.getString(curser.getColumnIndex("age"))
var gent=curser.getString(curser.getColumnIndex("gent"))
contentValues.put("id",id)
contentValues.put("name",name)
contentValues.put("age",age)
contentValues.put("gent",gent)
}while (curser.moveToNext())
curser.close()
idreadtext.text=contentValues.toString()
}
}
}
fun insertValues(id:Int){
contentValues.put("name",dummyDataArray[id].name)
contentValues.put("age",dummyDataArray[id].age)
contentValues.put("gent",dummyDataArray[id].gent)
myDatabase!!.insert("MyTable","",contentValues)
contentValues.clear()
Toast.makeText(this,"数据已添加",Toast.LENGTH_SHORT).show()
}
fun queryData(projection:Array<String>?,selection:String,selectionArg:Array<String>,sorOrder:String):Cursor{
val db=SQLiteQueryBuilder()
db.tables="MyTable"
var curser=db.query(myDatabase,projection,selection,selectionArg,null,null,sorOrder)
return curser
}
}
在queryData这个函数里就是查找数据的方法,基本都是固定格式照抄就行。因为手机输入法换成中文很麻烦,在数据库里我又新加进两个英文名字的版主。启动手机app后我们先点一下添加数据的按钮,然后在文本框里输入版主的名字就可以查找到版主的资料了,版主们的资料是我胡编的啊,可别当真 。效果图为:
|