Home > Net >  Kotlin: How to set text in textview from a row of data from SQLite
Kotlin: How to set text in textview from a row of data from SQLite

Time:04-04

Intent: Retrieve a random single row of data from SQLite table and display in activity english word on that row of data.

Case: Lack of knowledge on how to display it in the activity

Expected Result: TextView displaying a random word using "SELECT * FROM tbl_word ORDER BY RANDOM() LIMIT 1"

Note: RANDOM doesnt work in kotlin android studio

Main activity where I want word to show using text view ENWord

package com.example.ankiapp

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import kotlin.system.exitProcess

private lateinit var enWord: TextView
private lateinit var sqliteHelper: SQLiteHelper
private var adapter: WordAdapter?=null

class ENWordScreen : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_enword_screen)

    initView()
    sqliteHelper= SQLiteHelper(this)
    val word = sqliteHelper.getRandomWord()
    enWord.setText("")//stuck here <<<<<


    //ホーム画面に遷移ボタン
    val button = findViewById<Button>(R.id.backBtn1)//ホーム画面に戻る
    button.setOnClickListener{
        val intent = Intent(this,HomeScreen::class.java)
        startActivity(intent)
    }
    //アプリを終了するメソッド
    val quitButton = findViewById<Button>(R.id.quitBtn)
    quitButton.setOnClickListener{
        finish()
        exitProcess(0);
    }



}
private fun initView(){
    enWord = findViewById(R.id.etEngWord)
}

}

SQLiteHelper class

fun getRandomWord():ArrayList<WordModel>{//1つのランダムデータを取得メソッド
    val randomWordModel: ArrayList<WordModel> = ArrayList()
    val selectQuery = "SELECT * FROM "   TBL_WORD  " ORDER BY RANDOM() LIMIT 1"
    val db = this.readableDatabase

    val cursor: Cursor =  db.rawQuery(selectQuery, null)

    var id: Int
    var date: String
    var engword: String
    var jpword: String
    var time: Int
    var answer: Int

    if (cursor.moveToFirst())
        do {
            id = cursor.getInt(cursor.getColumnIndexOrThrow("id"))
            date = cursor.getString(cursor.getColumnIndexOrThrow("date"))
            engword = cursor.getString(cursor.getColumnIndexOrThrow("englishWord"))
            jpword = cursor.getString(cursor.getColumnIndexOrThrow("jpnWord"))
            time = cursor.getInt(cursor.getColumnIndexOrThrow("time"))
            answer = cursor.getInt(cursor.getColumnIndexOrThrow("answer"))

            val randomWord = WordModel(id, date, engword, jpword, time, answer)
            randomWordModel.add(randomWord)
        } while (cursor.moveToNext())

    cursor.close()
    db.close()

    return randomWordModel
}

WordModel Class

    package com.example.ankiapp
import android.os.Parcelable
import java.util.*
import kotlinx.parcelize.Parcelize


@Parcelize data class WordModel(
    var id: Int? = null,
    var date: String = "",
    var engword: String = "",
    var jpword: String = "",
    var time: Int = 0,
    var answer: Int = 0
) : Parcelable

WordAdapter class

class WordAdapter:RecyclerView.Adapter<WordAdapter.WordViewHolder>() {

private var wordList: ArrayList<WordModel> = ArrayList()
private var onClickItem: ((WordModel) -> Unit)?=null
private var onClickDeleteItem: ((WordModel) -> Unit)?=null
fun addItems(items:ArrayList<WordModel>){
    this.wordList = items
    notifyDataSetChanged()
}
//レコードを削除メソッドを呼び出す
fun setOnClickDeleteItem(callback: (WordModel) -> Unit){
    this.onClickDeleteItem = callback
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = WordViewHolder(
    LayoutInflater.from(parent.context).inflate(R.layout.card_items_word,parent,false)
)
override fun onBindViewHolder(holder: WordViewHolder, position: Int) {
    val word = wordList[position]
    holder.bindView(word)
    holder.itemView.setOnClickListener{onClickItem?.invoke(word)}
    holder.btnDelete.setOnClickListener{onClickDeleteItem?.invoke(word)}
}

override fun getItemCount(): Int {
    return wordList.size
}

class WordViewHolder(var view: View):RecyclerView.ViewHolder(view){

    var id = view.findViewById<TextView>(R.id.tvID)
    var date = view.findViewById<TextView>(R.id.tvDate)
    var jpword = view.findViewById<TextView>(R.id.tvJpWord)
    var engword = view.findViewById<TextView>(R.id.tvEngWord)
    var btnDelete = view.findViewById<Button>(R.id.deleteBtn)



    fun bindView(word:WordModel){
        id.text = word.id.toString()
        date.text = word.date
        jpword.text = word.jpword
        engword.text = word.engword
    }
}

}

CodePudding user response:

So you want something like this

sqliteHelper= SQLiteHelper(this)
val word = sqliteHelper.getRandomWord()
enWord.setText(word[0].engword)
  • Related