I'm trying to put the itemList as array which is fetching from other function, and put it into Hash Map. After the loop finished, I try to print the content which is wrong.
class ExpandableItemClass : AppCompatActivity() {
private var expandableListAdapter: ExpandableListViewAdapter? = null
private lateinit var titleList : List<String>
private lateinit var myHasMap: HashMap<String, List<String>>
private var itemList : ArrayList<String> ? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_pobitrota)
titleList = ArrayList()
myHasMap = HashMap()
itemList = ArrayList()
getAllTitles()
expandableListAdapter = ExpandableListViewAdapter(this@PobitrotaActivity, titleList, myHasMap)
pobitrota_recyclerListView.setAdapter(expandableListAdapter)
}
private fun getAllTitles() {
var pRef = FirebaseDatabase.getInstance().reference.child("Titles")
pRef.addValueEventListener(object : ValueEventListener{
override fun onCancelled(error: DatabaseError) {
}
override fun onDataChange(snapshot: DataSnapshot) {
if(snapshot.exists()){
(chapterList as ArrayList<String>).clear()
for (item in snapshot.children){
var subItem = item.getValue(Title::class.java)
if(subItem!!.getMainTitleId().equals("-M_FyZJbyXf3fLFyEuiT")){
(titleList as ArrayList<String>).add(subItem.getTitleName())
getAllItems(item.key!!)
}
}
}
}
})
}
private fun getAllItems(titleId: String) {
var pRef = FirebaseDatabase.getInstance().reference.child("DropdownItems")
pRef.addValueEventListener(object : ValueEventListener{
override fun onCancelled(error: DatabaseError) {
}
override fun onDataChange(snapshot: DataSnapshot) {
if(snapshot.exists()){
dropdownList!!.clear()
for (item in snapshot.children){
var stdItem = item.getValue(Item::class.java)
if(stdItem!!.getTitleId().equals(titleId)){
itemList!!.add(stdItem.getItemName())
}
}
while(index < titleList.size){
myHasMap.put(titleList[index], itemList!!)
break
}
index = index + 1
for(key in myHasMap.keys){
Log.d("TAG", key+" => "+myHasMap[key])
}
expandableListAdapter!!.notifyDataSetChanged()
}
}
})
}
}
Here is the result like below:
//1st iterator:
title1 => [item1, item2, item3]
//2nd iterator:
title1 => [item4, item5]
title2 => [item4, item5]
//3rd iterator:
title1 => [item6, item7]
title2 => [item6, item7]
title3 => [item6, item7]
Here is the result and it is correct. I hope so:
//1st iterator:
title1 => [item1, item2, item3]
//2nd iterator:
title1 => [item1, item2, item3]
title2 => [item4, item5]
//3rd iterator:
title1 => [item1, item2, item3]
title2 => [item4, item5]
title3 => [item6, item7]
I am guessing my problem is that hasmap can't save the data added to the hasmap.
What is the correct way to add data into hashmap? How can I get the right results?
Here is example how you can add an arrayList in other array of bundle
val items = eventData.get(EVENT_PARAM_CART_ITEM) as ArrayList<Item>
var arrayOf = arrayListOf<Bundle>()
for ((index, item) in items.withIndex()) {
val itemJeggings = Bundle().apply {
putString(FirebaseAnalytics.Param.ITEM_ID, items[index].itemId.toString())
putString(FirebaseAnalytics.Param.ITEM_BRAND, items[index].brand)
putLong(FirebaseAnalytics.Param.QUANTITY, items[index].quantity?.toLong()!!)
putString(FirebaseAnalytics.Param.CURRENCY, items[index].currency)
putDouble(FirebaseAnalytics.Param.PRICE, items[index].price?.toDouble()!!)
}
arrayOf.add(itemJeggings)
}