When trying to implement an ExpandableListView to my Android Studio app, I am unable to show a groups children. When I click on the Group in the expandable list, I used debugging logs to show that it is Expanding and Collapsing, but the children are not showing.
When checking the debug to see if getChildrenCount is detecting the children, it properly populates the correct amount of children in the group.
When debugging I also have noticed that getChild and getChildView are not getting called at all.
package wig.managers
import android.content.Context
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseExpandableListAdapter
import android.widget.TextView
import wig.R
import wig.models.entities.Ownership
import wig.models.responses.Borrowers
class BorrowersExpandableListAdapter(
private val context: Context,
private val borrowersList: List<Borrowers>
) : BaseExpandableListAdapter() {
override fun getGroup(groupPosition: Int): Any {
Log.d("Borrowers", "getGroup: " + borrowersList[groupPosition].borrower.borrowerName)
return borrowersList[groupPosition]
}
override fun getChild(groupPosition: Int, childPosition: Int): Any {
Log.d("Borrowers", "getChild: " + borrowersList[groupPosition].ownerships[childPosition].customItemName)
return borrowersList[groupPosition].ownerships[childPosition]
}
override fun getGroupCount(): Int {
Log.d("Borrowers", "getGroupCount : " + borrowersList.size)
return borrowersList.size
}
override fun getChildrenCount(groupPosition: Int): Int {
Log.d("Borrowers", "getChildrenCount: " + borrowersList[groupPosition].ownerships.size)
return borrowersList[groupPosition].ownerships.size
}
override fun getGroupId(groupPosition: Int): Long {
Log.d("Borrowers", "getGroupId: " + groupPosition.toLong())
return groupPosition.toLong()
}
override fun getChildId(groupPosition: Int, childPosition: Int): Long {
Log.d("Borrowers", "getChildId: " + childPosition.toLong())
return childPosition.toLong()
}
override fun hasStableIds(): Boolean {
Log.d("Borrowers", "hasStableIds: false")
return false
}
override fun getGroupView(groupPosition: Int, isExpanded: Boolean, convertView: View?, parent: ViewGroup?): View {
Log.d("Borrowers", "getGroupView called")
val convertView = convertView ?: LayoutInflater.from(context).inflate(R.layout.borrower_list_group, parent, false)
val txtBorrowerName = convertView.findViewById<TextView>(R.id.txtBorrowerName)
val borrower = getGroup(groupPosition) as Borrowers
txtBorrowerName.text = borrower.borrower.borrowerName
return convertView
}
override fun getChildView(groupPosition: Int, childPosition: Int, isLastChild: Boolean, convertView: View?, parent: ViewGroup?): View {
Log.d("Borrowers", "getChildView called")
val convertView = convertView ?: LayoutInflater.from(context).inflate(R.layout.ownership_list_item, parent, false)
val txtOwnershipDescription = convertView.findViewById<TextView>(R.id.txtOwnershipDescription)
val ownership = getChild(groupPosition, childPosition) as Ownership
txtOwnershipDescription.text = ownership.customItemName
return convertView
}
override fun isChildSelectable(groupPosition: Int, childPosition: Int): Boolean {
Log.d("Borrowers", "isChildSelectable: true")
return true
}
}
package wig.activities.loggedin
import android.os.Bundle
import android.util.Log
import android.widget.ExpandableListView
import androidx.lifecycle.lifecycleScope
import kotlinx.coroutines.launch
import wig.activities.base.Settings
import wig.managers.BorrowersExpandableListAdapter
class CheckedOut : Settings() {
private lateinit var expandableListView: ExpandableListView
private lateinit var adapter: BorrowersExpandableListAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setScreenOrientation()
setCheckedOutBindings()
expandableListView = checkedOutBinding.searchTableLayout
setOnClickListeners()
getInventory()
}
private fun setOnClickListeners() {
checkedOutBinding.topMenu.icScanner.setOnClickListener { startActivityScanner() }
checkedOutBinding.topMenu.icSettings.setOnClickListener { startActivitySettings() }
checkedOutBinding.topMenu.icCheckedOut.setOnClickListener { startActivityCheckedOut() }
checkedOutBinding.topMenu.icInventory.setOnClickListener { startActivityInventory() }
checkedOutBinding.returnAllButton.setOnClickListener { returnAllButton() }
}
// getBorrowedItems returns all of the borrowed items and their borrowers
private fun getInventory() {
lifecycleScope.launch {
val response = api.borrowerGetInventory()
if (response.success) {
val borrowers = response.borrowers
adapter = BorrowersExpandableListAdapter(this@CheckedOut, borrowers)
expandableListView.setAdapter(adapter)
expandableListView.expandGroup(0)
}
}
}
// returnAllButton handles the button click of Return All
private fun returnAllButton() {
// TODO
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp">
<TextView
android:id="@+id/txtBorrowerName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp">
<TextView
android:id="@+id/txtOwnershipDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<include layout="@layout/top_menu"
android:id="@+id/top_menu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@id/top_menu"
app:layout_constraintBottom_toBottomOf="parent"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/borrowed_out"
android:textSize="40sp"
android:layout_gravity="center_horizontal"
/>
<ScrollView
android:id="@+id/search_table"
android:layout_width="match_parent"
android:layout_height="400dp"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:background="@drawable/textbox_background"
android:padding="2dp"
android:contentDescription="@string/table_description"
android:visibility="visible">
<ExpandableListView
android:id="@+id/search_table_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:groupIndicator="@null">
</ExpandableListView>
</ScrollView>
<Button
android:id="@+id/return_all_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/return_all"
android:layout_gravity="center"/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
@Serializable
data class Borrowers(
val borrower: Borrower,
val ownerships: MutableList<Ownership>
)
I have tried debugging. I am expecting my child views to show when expanding a group.