使用循环[kotlin代码]将arrayList添加到HashMap中
我试图将itemList作为从其他函数获取的数组,并将其放入哈希映射中。循环完成后,我尝试打印错误的内容使用循环[kotlin代码]将arrayList添加到HashMap中,kotlin,while-loop,hashmap,iterator,Kotlin,While Loop,Hashmap,Iterator,我试图将itemList作为从其他函数获取的数组,并将其放入哈希映射中。循环完成后,我尝试打印错误的内容 class ExpandableItemClass : AppCompatActivity() { private var expandableListAdapter: ExpandableListViewAdapter? = null private lateinit var titleList : List<String> private lat
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()
}
}
})
}
}
这是结果,它是正确的。我希望如此:
//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]
我猜我的问题是hasmap无法保存添加到hasmap的数据
向hashmap添加数据的正确方法是什么?如何获得正确的结果?以下是如何在其他捆绑包数组中添加arrayList的示例
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)
}
val items=eventData.get(EVENT_PARAM_CART_ITEM)作为ArrayList
var arrayOf=arrayListOf()
对于items.withIndex()中的((索引,项){
val itemJeggings=Bundle()。应用{
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,项[索引].CURRENCY)
putDouble(FirebaseAnalytics.Param.PRICE,items[index].PRICE?.toDouble()!!)
}
添加的阵列(项目集合)
}
以下是我没有足够的时间来解决整个问题,我想你会明白这一点
fun main() {
// initialize the hashmap
val hash = hashMapOf<String, MutableList<String>>()
// put the keys with empty lists into the hashmap
hash.put("bro", mutableListOf())
hash.put("sis", mutableListOf())
// add items to the value (the list) of existing keys
hash.get("bro")?.add("rahat")
hash.get("sis")?.add("mia")
hash.get("bro")?.add("tech")
hash.get("sis")?.add("user")
// print size and content
println("Hash size is ${hash.size.toString()}")
println(hash)
}
什么是
标题列表
变量?它来自哪里?它与itemList
有何关系?这很难回答,因为您提供的代码无法生成此输出。请给出一个最小且可重复的示例。我的第一个猜测是,在将项目列表添加到HashMap
后,您会对其进行变异。这也将更改您放入HashMap
中的项目列表
。Adam Millerchip我已更新了我的问题。您是否在迭代之间对项目列表
进行变异或重新分配?否。添加HashMap数据后,我刚刚调用adapterbro,没有得到预期的输出。你还有别的办法可以帮我吗?
fun main() {
// initialize the hashmap
val hash = hashMapOf<String, MutableList<String>>()
// put the keys with empty lists into the hashmap
hash.put("bro", mutableListOf())
hash.put("sis", mutableListOf())
// add items to the value (the list) of existing keys
hash.get("bro")?.add("rahat")
hash.get("sis")?.add("mia")
hash.get("bro")?.add("tech")
hash.get("sis")?.add("user")
// print size and content
println("Hash size is ${hash.size.toString()}")
println(hash)
}
Hash size is 2
{sis=[mia, user], bro=[rahat, tech]}