0

我想在我的應用程序來實現的ExpandableListView,但它沒有顯示出來。ExpandableListView沒有出現在我的活動

我仔細檢查了:

  1. 正確的命名和指對所有我的三個XML佈局(用於ExpandableListView,表頭和列表項)的,

  2. 調試的,看我的列表正在填充prepareListdata()方法(它們是)。

我覺得問題在我ExpandableListAdapterClass,作爲 類參數listDataHeader : ArrayList<String>, listDataChild : HashMap<String, ArrayList<String>>越來越着色爲 波浪灰線不使用。

如何正確地告訴我的應用程序,這些都是等於我的活動成員變量?還是有什麼明顯的我失蹤?

這裏是我的代碼:

適配器

class ExpandableListAdapter (context: Context, listDataHeader : ArrayList<String>, listDataChild : HashMap<String, ArrayList<String>>) : BaseExpandableListAdapter() { 


     var listDataHeader = arrayListOf<String>() //list sub-headers 

     var listDataChild = HashMap<String, List<String>>() //list sub-points 

     private val inflater: LayoutInflater = LayoutInflater.from(context) 

     override fun getGroup(groupPosition: Int): Any { 
      return this.listDataHeader[groupPosition] 
     } 

     override fun isChildSelectable(p0: Int, p1: Int): Boolean { 
      return true 
     } 

     override fun hasStableIds(): Boolean { 
      return false 
     } 

     override fun getGroupView(groupPosition: Int, isExpanded: Boolean, convertView: View?, parent: ViewGroup?): View? { 
      val headerTitle: Int = getGroup(groupPosition) as Int 
      if (convertView == null) { 
       inflater.inflate(R.layout.main_list_group_header, parent, false) 
      } 

      val listHeader = convertView?.findViewById<TextView>(R.id.list_header) 
      listHeader?.setTypeface(null, Typeface.BOLD) 
      listHeader?.setText(headerTitle) 

      return convertView 
     } 

     override fun getChildrenCount(groupPosition: Int): Int { 
      return this.listDataChild[this.listDataHeader[groupPosition]]!!.size 
     } 

     override fun getChild(groupPosition: Int, childPosition: Int): Any { 
      return this.listDataChild[this.listDataHeader[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? { 
      val childText: Int = getChild(groupPosition, childPosition) as Int 
      if (convertView == null) { 
       inflater.inflate(R.layout.main_list_item, parent, false) 
      } 

      val textListChild = convertView?.findViewById<TextView>(R.id.list_item) 
      textListChild?.setText(childText) 
      return convertView 
     } 

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

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

MainActivity和prepareListData的其餘部分()函數

class MainListActivity : AppCompatActivity() { 

    lateinit var adapter : ExpandableListAdapter 
    val listDataHeader = arrayListOf<String>() 
    val listDataChild = HashMap<String, ArrayList<String>>() 

    override fun onCreate(savedInstanceState: Bundle?) { 
     super.onCreate(savedInstanceState) 
     setContentView(R.layout.activity_mainlistactivity) 
     prepareListData() 
     adapter = ExpandableListAdapter(this, listDataHeader, listDataChild) 
     expandable_list.setAdapter(adapter) 
    } 

    private fun prepareListData() { 

     listDataHeader.add("Gathering data") 
     listDataHeader.add("Simulate") 

     val gatheringDataChildList = arrayListOf<String>() 
     gatheringDataChildList.add("Record data: video + accelerometer") 

     val simulateChildList = arrayListOf<String>() 
     simulateChildList.add("Driving simulator") 

     listDataChild.put(listDataHeader[0], gatheringDataChildList) 
     listDataChild.put(listDataHeader[1], simulateChildList) 
    } 

這裏是什麼,我試圖達到的截圖: enter image description here

+0

那麼,你究竟想要得到填充?有色物品還是別的東西?意味着你的觀點在活動中是空的? –

+0

@AjayPandya是的。我的標題和子項都不顯示。視圖是完全空的。 – lidkxx

+0

@lidkxx是'expandable_list''RecyclerView'?如果是這樣,你需要指定'layoutManager'(最有可能的'LinearLayoutManager') – rafal

回答

0

你是非常接近解決問題:)

class ExpandableListAdapter (context: Context, listDataHeader : ArrayList<String>, listDataChild : HashMap<String, ArrayList<String>>) : BaseExpandableListAdapter() { 


     var listDataHeader = arrayListOf<String>() //list sub-headers 

     var listDataChild = HashMap<String, List<String>>() //list sub-points 
     //... 
} 

您指定構造函數的參數的第一行,但你不使用它們。 (var listDataHeader是類的屬性,這意味着它的東西比構造函數的參數listDataHeader不同)

你可能想是這樣的:

class ExpandableListAdapter (context: Context, private val listDataHeader : ArrayList<String>, private val listDataChild : HashMap<String, ArrayList<String>>) : BaseExpandableListAdapter() { 
//no declaration of another fields 

//rest of adapter code here... 
} 

前請構造函數的參數注意到private val。這些是在構造函數中直接聲明的類的屬性。你可以閱讀更多關於他們在這裏:https://kotlinlang.org/docs/reference/classes.html

+0

非常感謝您的幫助!我想,這就是我一直在尋找(不能告訴100%的,因爲我得到一些討厭的圖書館「/system/lib64/libhwuibp.so」沒有找到,現在發送SIG 9錯誤,但是這似乎是另一個問題)。我想,有一點文件從來不會殺死任何人。 – lidkxx