Solicito aos mestre do android uma ajuda substancial.
Primeiro quero dizer que sou novato dos novato no assunto.
Estou aprendendo observando pelas duvidas que vocês resolvem…
Faz um mês que estou tentando colocar checkbox na listview e não consigo…
Por favor podem me ajudar abaixo o código
Obrigado
package com.example.sqlite_up1
import android.annotation.SuppressLint
import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.CheckedTextView
import android.widget.ListView
import android.widget.SimpleCursorAdapter
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
class ListaActivity : AppCompatActivity() {
private lateinit var names: Array<String?>
var context: Context? = null
var inflter: LayoutInflater? = null
var value: String? = null
private var dbManager: DBManager? = null
private var listView: ListView? = null
private var adapter: SimpleCursorAdapter? = null
private val from = arrayOf(DatabaseHelper._ID, DatabaseHelper.SUBJECT)
private val to = intArrayOf(R.id.id, R.id.title)
@SuppressLint("WrongViewCast")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_lista)
dbManager = DBManager(this)
dbManager!!.open()
val cursor = dbManager!!.fetch()
listView = findViewById<View>(R.id.list_view) as ListView
adapter = SimpleCursorAdapter(this, R.layout.activity_view_record, cursor, from, to, 0)
adapter!!.notifyDataSetChanged()
listView!!.adapter = adapter
// ListView contem as informaçoes do SQLite…//
// sem a parte de baixo esta trazendo os dados , o que não consigo é carregar o checkbox-----------------------------------------------------------------------------------------//
//fun CustomAdapter(context: Context, names: Array<String?>) {
context = context
names = names
inflter = LayoutInflater.from(context)
}
@SuppressLint("ViewHolder")
fun getView(position: Int, view: View, parent: ViewGroup?): View? {
var view = view
view = inflter!!.inflate(R.layout.list_view_items, null)
val simpleCheckedTextView = view.findViewById<CheckedTextView>(R.id.simpleCheckedTextView)
simpleCheckedTextView.text = names[position]
// perform on Click Event Listener on CheckedTextView
simpleCheckedTextView.setOnClickListener { v: View? ->
if (simpleCheckedTextView.isChecked) {
// set cheek mark drawable and set checked property to false
value = "un-Checked"
simpleCheckedTextView.setCheckMarkDrawable(0)
simpleCheckedTextView.isChecked = false
} else {
// set cheek mark drawable and set checked property to true
value = "Checked"
simpleCheckedTextView.setCheckMarkDrawable(R.drawable.checked)
simpleCheckedTextView.isChecked = true
}
Toast.makeText(context, value, Toast.LENGTH_SHORT).show()
}
return view
}