Я новичок в Котлине и сейчас изучаю этот язык кодирования. На данный момент я застрял в проблеме, что в моем многоуровневом расширяемом списке просмотра второго уровня groupPosition всегда равно "0". Представление всегда показывает 3 раза одни и те же объекты, но это должны быть 3 разных объекта.
Данные поступают из локальной базы данных SQL-Lite, и Databasequery работает нормально.
Вот мойкод, я надеюсь, вы можете помочь мне!
class pflegeRabGru :AppCompatActivity(){
val ctx:Context = this
internal var expandableListView: ExpandableListView?=null
internal var listAdapter: ExpandableListAdapter?=null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_pflege_rabgru)
expandableListView= findViewById(R.id.expRabListView)
if (expandableListView !=null){
listAdapter=adapter_RabGruKat(this)
expandableListView!!.setAdapter(listAdapter)
}
}
Первый адаптер adapter_RabGruKat
class adapter_RabGruKat internal constructor(private val context: Context):BaseExpandableListAdapter() {
val lclDb=lclDBHelper(context)
val listRabgru = ArrayList<ArtRabGru>()
val listRabFields = ArrayList<RabGruFields>()
val listCats = ArrayList<Categorys>()
val listReadedRabgru= getRabForGroups()
val listReadedRabKats = getKatsForGroups()
override fun getChild(groupPosition: Int, childPosition: Int):Int{
return childPosition
}
override fun getChildId(groupPosition: Int, childPosition: Int): Long{
return childPosition.toLong()
}
override fun getChildView(groupPosition: Int, childPosition: Int, isLastChild: Boolean, convertView: View?, parent:ViewGroup):View{
var convertView = convertView
var subObjects = ExpandableListView(context)
subObjects!!.setAdapter(adapter_RabGruKatChilds(context,childPosition.toLong(),getGroup(groupPosition).get(groupPosition).WAHID))
subObjects!!.setGroupIndicator(null)
return subObjects
}
override fun getChildrenCount(groupPosition: Int): Int {
return listCats.size
}
override fun getGroup(groupPosition: Int):ArrayList<ArtRabGru>{
return listRabgru
}
override fun getGroupCount(): Int {
return listRabgru.size
}
override fun getGroupId(groupPosition: Int): Long {
return groupPosition.toLong()
}
override fun getGroupView(groupPosition: Int,isExpanded: Boolean, convertView: View?, parent: ViewGroup): View {
var convertView = convertView
val groupTitle = getGroup(groupPosition).get(groupPosition).WAH_RABGRU + " " + getGroup(groupPosition).get(groupPosition).WAH_RABGRUTEXT
val groupId = getGroup(groupPosition).get(groupPosition).WAHID
if (convertView==null){
val layoutInflater = this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
convertView = layoutInflater.inflate(R.layout.parent_pflege_rabgru, null)
}
val listRabGruTitle = convertView!!.findViewById<TextView>(R.id.txtRabgruName)
val listRabId = convertView!!.findViewById<TextView>(R.id.txtRabGruId)
listRabGruTitle.text=groupTitle
listRabId.text=groupId.toString()
return convertView
}
override fun hasStableIds(): Boolean {
return true
}
override fun isChildSelectable(groupPosition: Int, childPosition: Int):Boolean {
return true
}
fun getRabForGroups():ArrayList<ArtRabGru>{
lclDb.readAllArtRabGru().forEach() {
listRabgru.add(ArtRabGru(it.WAHID,it.WAH_RABGRU,it.WAH_RABGRUTEXT))
}
return listRabgru
}
fun getKatsForGroups():ArrayList<Categorys>{
lclDb.readAllCategory().forEach {
listCats.add(Categorys(it.KATID,it.KATNAME))
}
return listCats
}
}
и Второй уровень адаптера
class adapter_RabGruKatChilds internal constructor(private val context:Context, private val childPositionOtherAdapter:Long,private val selectedRabgru:Long) : BaseExpandableListAdapter() {
val lclDb = lclDBHelper(context)
var listCats = ArrayList<Categorys>()
val readCats = getKatsForGroups()
val rabFields = ArrayList<RabGruFields>()
val readRabFields = getFieldRabs()
override fun getChild(childPosition: Int, groupPosition: Int):RabGruFields {
return rabFields.get(childPosition)
}
override fun getChildId(groupPosition:Int, childPosition:Int): Long {
return childPosition.toLong()
}
override fun getChildView(groupPosition: Int, childPosition: Int, isLastChild: Boolean, convertView: View?, parent:ViewGroup): View {
var convertView = convertView
val fieldId= getChild(childPosition,groupPosition).RABFIELDID
val fieldName= getChild(childPosition,groupPosition).RABFIELDTEXT
if (convertView==null){
val layoutInflater = this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
convertView=layoutInflater.inflate(R.layout.pflegefieldsitems, null)
}
val txtfieldId = convertView!!.findViewById<TextView>(R.id.txtFieldId)
val txtfieldName = convertView!!.findViewById<TextView>(R.id.txtFieldName)
txtfieldId.text=fieldId.toString()
txtfieldName.text=fieldName
return convertView
}
override fun getChildrenCount(groupPosition: Int): Int {
return rabFields.size
}
override fun getGroup(groupPosition: Int):Categorys{
Log.i("INFO", "GroupPosition in getGroupt="+ groupPosition.toString())
return listCats.get(groupPosition)
}
override fun getGroupCount(): Int {
return listCats.size
}
override fun getGroupId(groupPosition: Int):Long {
return groupPosition.toLong()
}
override fun getGroupView(groupPosition: Int,isExpanded:Boolean, convertView:View?, parent:ViewGroup): View? {
var convertView = convertView
val groupCatId = getGroup(groupPosition).KATID
val groupTitle = getGroup(groupPosition).KATNAME
if(convertView==null){
val layoutInflater = this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
convertView = layoutInflater.inflate(R.layout.parent_pflege_rabgru, parent,false)
}
val txtCatId = convertView!!.findViewById<TextView>(R.id.txtRabGruId)
val txtCatName = convertView!!.findViewById<TextView>(R.id.txtRabgruName)
txtCatId.text= groupCatId.toString()
txtCatName.text = groupTitle
Log.i("INFO", "GroupPosition in getGroupView=" + groupPosition.toString())
return convertView
}
override fun hasStableIds(): Boolean {
return true
}
override fun isChildSelectable(groupPosition: Int, childPosition: Int): Boolean {
return true
}
fun getKatsForGroups():ArrayList<Categorys>{
listCats.clear()
lclDb.readAllCategory().forEach {
listCats.add(Categorys(it.KATID,it.KATNAME))
}
return listCats
}
fun getFieldRabs(): ArrayList<RabGruFields>{
rabFields.clear()
lclDb.readAllRabFields(selectedRabgru).forEach{
rabFields.add(RabGruFields(it.RABID, it.RABGRUID,it.RABFIELDID,it.RABFIELDMANDATORY,it.RABFIELDSHOW,it.RABFIELDTEXT,it.RABFIELDRANDOMIZE))
}
return rabFields
}
}