skip to Main Content

-if the data in editTextText2 is in the database, I want to print it to textview.
-I’ve read a lot of articles and watched videos, but I couldn’t adapt it to my own. most of them are not in the kotlin language, and I am a novice.
-they printed out the entire database or searched for a certain word. i did these things.i want to search for the data in editTextText2.(the search will be in column col_name)

MainActivity.kt

binding.button3.setOnClickListener{
            var data = db.readData()
            binding.textView.text =""
            /*for (i in 0 until data.size){
                binding.textView.append(data.get(i).id.toString()+" "
                        +data.get(i).adsoyad+" "+data.get(i).yasi+"n")}*/
            for (i in 0 until data.size){
               if (binding.editTextText2.toString()==data.get(i).adsoyad){binding.textView.append(data.get(i).adsoyad+"n")}
                else{binding.textView.text="yeni"+"n"}

            }

    }

DataBaseHelper.kt

import android.content.ContentValues
import android.content.Context
import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteOpenHelper
import android.widget.Toast

val database_name = "veritabanim"
val table_name = "kullanicilar"
val col_name = "adisoyadi"
val col_age = "yasi"
val col_id = "id"
class DataBaseHelper (var context: Context):SQLiteOpenHelper(context,
    database_name,null,1) {
    override fun onCreate(p0: SQLiteDatabase?) {
        var createTable = " CREATE TABLE "+ table_name+"("+
                col_id +" INTEGER PRIMARY KEY AUTOINCREMENT,"+
                col_name +" VARCHAR(256),"+
                col_age +" INTEGER)"
        p0?.execSQL(createTable)
    }

    override fun onUpgrade(p0: SQLiteDatabase?, p1: Int, p2: Int) {
        TODO("Not yet implemented")
    }
    fun insertData(kullanici: kullanici){
        val db =this.writableDatabase
        val cv = ContentValues()
        cv.put(col_name,kullanici.adsoyad)
        cv.put(col_age,kullanici.yasi)
        var sonuc =db.insert(table_name,null,cv)
        if (sonuc ==(-1).toLong()){
            Toast.makeText(context,"hatali",Toast.LENGTH_LONG).show()
        }else{
            Toast.makeText(context,"başsarili",Toast.LENGTH_LONG).show()
        }
    }

    fun readData(): MutableList<kullanici> {
        var liste:MutableList<kullanici> = ArrayList()
        val db = this.writableDatabase
        var sorgu = "Select * from "+table_name
        var sonuc = db.rawQuery(sorgu,null)
        if (sonuc.moveToFirst()){
            do {
                var kullanici = kullanici()
                kullanici.yasi = sonuc.getString(sonuc.getColumnIndexOrThrow(col_age)).toInt()
                kullanici.id = sonuc.getString(sonuc.getColumnIndexOrThrow(col_id)).toInt()
                kullanici.adsoyad = sonuc.getString(sonuc.getColumnIndexOrThrow(col_name))
                liste.add(kullanici)
            }while (sonuc.moveToNext())
        }
        sonuc.close()
        db.close()
        return liste
    }



}

kullanici.kt

class kullanici {
    var id:Int=0
    var adsoyad:String=""
    var yasi:Int=0
    constructor(adsoyad:String,yasi:Int){
        this.adsoyad =adsoyad
        this.yasi =yasi
    }
    constructor(){}
}

2

Answers


  1. enter image description here

    enter image description here

    For Sqlite control, you can write a control function in the DAO section, that is, in the operations section, as in the first photo, this function checks how many of that data there are. In my example I checked with id, you can check with other unique data. In the second picture, this control is being run, the bundle.id part there is my incoming data, you can save your own data and if the control data !=0 you can say print to textview. In this example, I use it for control so that the same data is not added when adding data to the cart.

    Login or Signup to reply.
  2. Okay so as you asked I am sharing a snippet of the necessary code here so that you could implement the same by watching this.

    First of all this is a function to search in database in DataBaseHelper.kt file.

    1.Scenario : You only want the exact word that you typed in the edittext.

        fun searchUser(name: String): ArrayList<String> {
            val db = this.readableDatabase
            val model: ArrayList<String> = ArrayList<String>()
            val cursor = db.rawQuery("SELECT * FROM user_details WHERE username =? ", arrayOf(name))
            if (cursor != null && cursor.moveToFirst()) {
                do {
                    addModelData(model, cursor)
                } while (cursor.moveToNext())
                cursor.close()
            }
            return model
        }
    

    Note: I have taken arraylist because my database can have same multiple names.You can customze as per your requirement.

    2nd Scenario:if you type app, it will return all entries which has app in it. eg. apple, app, application etc.

    For that you just need to change the query a bit.
    val cursor = db.rawQuery("SELECT * FROM user_details WHERE username LIKE "%$name%"",null)

    Now you call the function in your activity or fragment like this.I have used a recyclerView but as u have used textView I have customised the code for you.

       btnSearch.setOnClickListener{
               val str = edtSearch.text.toString()
               if (str.isNotEmpty()){
                   val searchData = dBhandler.searchUser(str)
                   txtName.text = searchData.toString()
               }
               else{
                   Toast.makeText(this,"No Matching entries found",Toast.LENGTH_SHORT).show()
               }
           }
    

    Manage the arrayList as per your requirement.You can return only String in Database Helper if you do not allow multiple names in database. Else If you do have multiple same name entries, would recommend to set those names in recyclerview instead of textView.

    In Short: Take Overview and manage accordingly. The main thing was the query that i gave you. Hope this helps

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search