notifyDataChanged()无法更新可扩展Listview数据

时间:2018-01-11 18:17:08

标签: android listview kotlin expandablelistview

我正在开发一个可扩展的列表视图,在我创建一个奖励广告后,我希望在用户观看奖励广告后添加一个可展开的列表视图,但该应用程序会因Android监视器错误而崩溃。这是代码......

class MainActivity : Activity(),RewardedVideoAdListener {

override fun onRewardedVideoAdClosed() = Unit

override fun onRewardedVideoAdLeftApplication() = Unit

override fun onRewardedVideoAdLoaded() = Unit

override fun onRewardedVideoAdOpened() = Unit

override fun onRewarded(rewardedLiterature: RewardItem?) {

    rewardedLiterature.apply {

        ParentList.add("New Articles")
        ParentList.reverse()
    }
}

override fun onRewardedVideoStarted() = Unit

override fun onRewardedVideoAdFailedToLoad(p0: Int) {
   Toast.makeText(this,"Please try again",Toast.LENGTH_SHORT).show()
}

private lateinit var childlist: MutableList<String>
private lateinit var ParentListItems: MutableMap<String, List<String>>
private lateinit var expandabilityView: ExpandableListView
private var ParentList: MutableList<String> = ArrayList()
 )
private var poems = arrayOf("Five","Six","Seven")
private var LoveName = arrayOf("Eight","Nine","Ten","Eleven")
private var moreLiterature = arrayOf("ZOne","zTwo","ZThree")
private var ByDefaultMessage = arrayOf("Items Loading")
private lateinit var mRewardedVideoAd : RewardedVideoAd

init {

    ParentList.add("Letters")
    ParentList.add("Poems")
    ParentList.add("Let's Chill!")
}

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

     mRewardedVideoAd = MobileAds.getRewardedVideoAdInstance(this)
     mRewardedVideoAd.rewardedVideoAdListener = this
     loadRewardedVideoAd()

     val moreContentButton = findViewById<FloatingActionButton>(R.id.moreContents)

      moreContentButton.setOnClickListener{

              if (mRewardedVideoAd.isLoaded) {
                  mRewardedVideoAd.show()
              }
      }

    ParentListItems = LinkedHashMap()
    for (HoldItem in ParentList) {
        when (HoldItem) {

            "Letters" -> loadChild(funnyLiterature)
            "Poems" -> loadChild(poems)
            "Let's Chill!" -> loadChild(LoveName)
            "New Articles" -> loadChild(moreLiterature)

            else -> loadChild(ByDefaultMessage)
        }

        ParentListItems.put(HoldItem, childlist)
    }
    expandabilityView = findViewById(R.id.expandableListView1)

    val expListAdapter = ListAdapter(
            this, ParentList, ParentListItems)
    expandabilityView.setAdapter(expListAdapter)
    expListAdapter.notifyDataSetChanged()
    expandabilityView.setOnChildClickListener {_, _, groupPosition, childPosition, _ ->


        val selected = expListAdapter.getChild(
                groupPosition, childPosition) as String

        val intent = Intent(this@MainActivity, DetailActivity::class.java)
        when (selected) {

            "ማትስ ተማሪ ለፍቅረኛዋ የፃፈችዉ ደብዳቤ" -> titleOfContent = detailContents[0]

            "የህግ ተመራቂ ለፍቅረኛዉ የፃፈዉ መልእክት" -> titleOfContent = detailContents[1]

            "የ‹ለምን አንተን ወደድኩህ?›ፍልስፍና" ->  titleOfContent = detailContents[2]

            "አጭር መልእክት" -> titleOfContent = detailContents[3]

        }
           private fun loadRewardedVideoAd() =
      mRewardedVideoAd.loadAd("ca-app-pub-xxxxxxx", AdRequest.Builder().build())
private fun loadChild(ParentElementsName: Array<String>) {
    childlist = ArrayList()
    for (model in ParentElementsName)
        childlist.add(model)
}

companion object {
    internal var titleOfContent: String ?= null
     private var detailContents = arrayOf("This","Is","a","Test")

这是适配器类 ...

 class ListAdapter(private val context: Activity, private val Items: List<String>,
                  private val ParentListItems: Map<String, List<String>>) : BaseExpandableListAdapter() {

    override fun getChild(groupPosition: Int, childPosition: Int): Any =
            ParentListItems[Items[groupPosition]]!![childPosition]

    override fun getChildId(groupPosition: Int, childPosition: Int): Long = childPosition.toLong()

    override fun getChildView(groupPosition: Int, childPosition: Int,
                              isLastChild: Boolean, ListView: View?, parent: ViewGroup): View {
        var myListView = ListView
        val contentPosition = getChild(groupPosition, childPosition) as String
        val inflater = context.layoutInflater
        if (myListView == null) myListView = inflater.inflate(R.layout.child_list_item, null)
        val item = myListView!!.findViewById<TextView>(R.id.textView1)
        item.text = contentPosition
        return myListView
    }

    override fun getChildrenCount(groupPosition: Int): Int =
            ParentListItems[Items[groupPosition]]!!.size

    override fun getGroup(groupPosition: Int): Any = Items[groupPosition]

    override fun getGroupCount(): Int = Items.size

    override fun getGroupId(groupPosition: Int): Long = groupPosition.toLong()

    override fun getGroupView(groupPosition: Int, isExpanded: Boolean,
                              ListView: View?, parent: ViewGroup): View {
        var myListView= ListView
        val allContents = getGroup(groupPosition) as String
        if (myListView == null) {
            val textInflater = context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
            myListView = textInflater.inflate(R.layout.parent_list_item, null)
        }
        val item = myListView!!.findViewById<TextView>(R.id.textView1)
        item.text = allContents
        return myListView
    }

    override fun hasStableIds(): Boolean = true

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

Android Monitor表示在 getChildrenCount函数的Adapter类中找到了Kotlin NullPoint Exception。

0 个答案:

没有答案