Я начал использовать фрагменты и все еще пытаюсь научиться правильно их использовать. В проекте, который я делаю, я хочу использовать recyclerView, который показывает список элементов, которые хранятся в базе данных. У меня возникли некоторые проблемы с этим, так как мой recyclerView не показывает ни один из элементов.
Фрагмент, на котором я хочу показать recyclerView, выглядит следующим образом:
package com.example.is4gestaointegrada.fragments
import android.content.Context
import android.net.Uri
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.is4gestaointegrada.Adapter.ArtigoAdapter
import com.example.is4gestaointegrada.DBHandlers.DBHandler
import com.example.is4gestaointegrada.MainActivity.Companion.dbHandler
import com.example.is4gestaointegrada.R
import kotlinx.android.synthetic.main.fragment_ver_artigos.view.*
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"
class VerArtigos : Fragment() {
private var param1: String? = null
private var param2: String? = null
private var listener: OnFragmentInteractionListener? = null
override fun onAttach(context: Context) {
super.onAttach(context)
if (context is OnFragmentInteractionListener) {
listener = context
} else {
throw RuntimeException(context.toString() + " must implement OnFragmentInteractionListener")
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
param1 = it.getString(ARG_PARAM1)
param2 = it.getString(ARG_PARAM2)
}
dbHandler = DBHandler(context!!, null, null, 1)
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view : View = inflater.inflate(R.layout.fragment_ver_artigos, container, false)
viewArtigos(view)
return view
}
override fun onDetach() {
super.onDetach()
listener = null
}
interface OnFragmentInteractionListener {
// TODO: Update argument type and name
fun onFragmentInteraction(uri: Uri)
}
private fun viewArtigos(view : View){
val artigosList = dbHandler.getArtigos(context!!)
val adapter = ArtigoAdapter(context!!, artigosList)
val rv : RecyclerView = view.rv_artigos
rv.layoutManager = LinearLayoutManager(context, RecyclerView.VERTICAL, false)
rv.adapter = adapter
}
companion object {
// TODO: Rename and change types and number of parameters
@JvmStatic
fun newInstance(param1: String, param2: String) =
VerArtigos().apply {
arguments = Bundle().apply {
putString(ARG_PARAM1, param1)
putString(ARG_PARAM2, param2)
}
}
}
}
Код функции обработчика такой:
fun getArtigos(mCtx : Context) : ArrayList<Artigo>{
val qry = "SELECT * FROM $DB_ARTIGO_TABLE"
val db = this.readableDatabase
val cursor = db.rawQuery(qry, null)
val artigos = ArrayList<Artigo>()
if (cursor.count == 0){
Toast.makeText(mCtx, "Não foram encontrados artigos", Toast.LENGTH_LONG).show()
} else {
while (cursor.moveToNext()){
val artigo = Artigo()
artigo.artigoNome = cursor.getString(cursor.getColumnIndex(DB_ARTIGO_NOME))
artigo.artigoAbreviada = cursor.getString(cursor.getColumnIndex(DB_ARTIGO_ABREVIADA))
artigo.artigoExtensa = cursor.getString(cursor.getColumnIndex(DB_ARTIGO_EXTENSA))
artigo.artigoFamiliaCodigo = cursor.getInt(cursor.getColumnIndex(DB_ARTIGO_FAMILIACODIGO))
artigo.artigoFamiliaDescricao = cursor.getString(cursor.getColumnIndex(DB_ARTIGO_FAMILIADESCRICAO))
artigo.artigoCodigoIVA = cursor.getInt(cursor.getColumnIndex(DB_ARTIGO_CODIGOIVA))
artigo.artigoPreco = cursor.getDouble(cursor.getColumnIndex(DB_ARTIGO_PRECO))
}
}
cursor.close()
db.close()
return artigos
}
Адаптер выглядит следующим образом:
package com.example.is4gestaointegrada.Adapter
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.example.is4gestaointegrada.DBModels.Artigo
import com.example.is4gestaointegrada.R
import kotlinx.android.synthetic.main.lo_artigos.view.*
class ArtigoAdapter (mCtx : Context, val artigos : ArrayList<Artigo>) : RecyclerView.Adapter<ArtigoAdapter.ViewHolder>(){
val mCtx = mCtx
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView){
val txtArtigoNome = itemView.txt_NomeArtigo
val txtArtigoDesc = itemView.txt_ArtigoDesc
val txtArtigoPreco = itemView.txt_ArtigoPreco
}
override fun onCreateViewHolder(p0: ViewGroup, p1: Int): ArtigoAdapter.ViewHolder {
val v = LayoutInflater.from(p0.context).inflate(R.layout.lo_artigos, p0, false)
return ViewHolder(v)
}
override fun getItemCount(): Int {
return artigos.size
}
override fun onBindViewHolder(p0: ArtigoAdapter.ViewHolder, p1: Int) {
val artigo : Artigo = artigos[p1]
p0.txtArtigoNome.text = artigo.artigoNome
p0.txtArtigoDesc.text = artigo.artigoAbreviada
p0.txtArtigoDesc.text = artigo.artigoPreco.toString()
}
}