• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps ExpandableListView on fragment

xCriminaL

Lurker
Hello World!

I have a big Problem with a fragment in my android project. I've tried to put an expandableListView inside my fragment and the app crashes when i navigate to this fragment. I hope u can help me.. (Using kotlin and lokking for answers sice 2-3 days -.-)

Here is my code of helpFragment.kt

Code:
override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
  
  
    ): View? {
        val view = inflater.inflate(R.layout.fragment_help, container, false)
      
val thema1: MutableList<String> = ArrayList()
        thema1.add("help")


        val thema2: MutableList<String> = ArrayList()
        thema2.add("help")


        val thema3: MutableList<String> = ArrayList()
        thema3.add("help")

        header.add("Thema 1")
        header.add("Thema 2")
        header.add("Thema 3")
      
        body.add(thema1)
        body.add(thema2)
        body.add(thema3)
      
        expandableListView.setAdapter(MyAdapter(context, expandableListView, header, body))

        return view
    }

    override fun onAttach(context: Context) {
        super.onAttach(context)

        getContext()


       
    }


(if needed code from myAdapter.kt)
Code:
class MyAdapter(var context: Context,var expandableListView : ExpandableListView,var header : MutableList<String>, var body : MutableList<MutableList<String>>) : BaseExpandableListAdapter(){
    override fun getGroup(groupPosition: Int): String {
        return header[groupPosition]
    }

    override fun isChildSelectable(groupPosition: Int, childPosition: Int): Boolean {
        return true
    }

    override fun hasStableIds(): Boolean {
        return false
    }

    override fun getGroupView(groupPosition: Int, isExpanded: Boolean, convertView: View?, parent: ViewGroup?): View? {
        var convertView = convertView
        if(convertView == null){
            val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
            convertView = inflater.inflate(R.layout.parent_layout,null)
        }
        val title = convertView?.findViewById<TextView>(R.id.tv_title)
        title?.text = getGroup(groupPosition)
        title?.setOnClickListener {
            if(expandableListView.isGroupExpanded(groupPosition))
                expandableListView.collapseGroup(groupPosition)
            else
                expandableListView.expandGroup(groupPosition)
            Toast.makeText(context, getGroup(groupPosition),Toast.LENGTH_SHORT).show()
        }
        return convertView
    }

    override fun getChildrenCount(groupPosition: Int): Int {
        return body[groupPosition].size
    }

    override fun getChild(groupPosition: Int, childPosition: Int): String {
        return body[groupPosition][childPosition]
    }

    override fun getGroupId(groupPosition: Int): Long {
        return groupPosition.toLong()
    }

    override fun getChildView(groupPosition: Int, childPosition: Int, isLastChild: Boolean, convertView: View?, parent: ViewGroup?): View? {
        var convertView = convertView
        if(convertView == null){
            val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
            convertView = inflater.inflate(R.layout.child_layout,null)
        }
        val title = convertView?.findViewById<TextView>(R.id.tv_title)
        title?.text = getChild(groupPosition,childPosition)
        title?.setOnClickListener {
            Toast.makeText(context, getChild(groupPosition,childPosition),Toast.LENGTH_SHORT).show()
        }
        return convertView
    }

    override fun getChildId(groupPosition: Int, childPosition: Int): Long {
        return childPosition.toLong()
    }

    override fun getGroupCount(): Int {
        return header.size
    }

}

I hope anyone can help me. Thanks for looking.
 
Last edited:
Please show the stack trace produced by your application crash.
 
I‘ve already changed this line
Code:
expandableListView.setAdapter(MyAdapter(context, expandableListView, header, body))
To
Code:
expandableListView.setAdapter(MyAdapter(this, expandableListView, header, body))
But I can’t use “this” inside of a fragment and don’t understand how I can code with “this“ inside a fragment. Is this way completely wrong?
 
Back
Top Bottom