Android kotlin: putParcelableArrayList(ArrayList<int>) 和 getParcelableArrayList<Int>() 不会工作
Android kotlin: putParcelableArrayList(ArrayList<int>) and getParcelableArrayList<Int>() wont work
我正在尝试将 ArrayList<Integer>
从片段传递到另一个片段,这是我的代码:
Kotlin 代码
companion object {
fun newInstance(categoryId: Int, brandsList: ArrayList<Int>): Fragment {
val fragment = CategoryAllAdsFragment()
fragment.arguments = Bundle()
fragment.arguments!!.putInt(Constant.CATEGORY_ID, categoryId)
fragment.arguments!!.putParcelableArrayList(Constant.BRANDS_LIST, brandsList)
return fragment
}
}
但上面写着:
Type mismatch.
Required: java.util.ArrayList !
Found: kotlin.collections.ArrayList /* = java.util.ArrayList */
当我试图阅读时也是如此。
Kotlin 代码
try {
val brandsList = arguments!!.getParcelableArrayList<Int>(Constant.BRANDS_LIST)
} catch (ex: Exception) {
throw Exception("brand list cannot be null")
}
它说:
Type argument is not within its bounds
Expected: Parcelable!
Found: Int
我用 Java
测试过它,它工作正常。
使用
putIntegerArrayList(String key, ArrayList value)
Inserts an ArrayList value into the mapping of this Bundle, replacing any
existing value for the given key
putIntegerArrayList(Constant.BRANDS_LIST, array)
点赞
getIntegerArrayList(String key)
Returns the value associated with the given key, or null if no mapping
of the desired type exists for the given key or a null value is
explicitly associated with the key.
extras.getIntegerArrayList(Constant.BRANDS_LIST)
您可以创建一个通用的 Parcelable class,其中包含一个类型为 (Any) 的变量
class BaseParcelable : Parcelable {
var value: Any
constructor(value: Any) {
this.value = value
}
constructor(parcel: Parcel) {
this.value = Any()
}
override fun writeToParcel(dest: Parcel?, flags: Int) {}
override fun describeContents(): Int = 0
companion object CREATOR : Parcelable.Creator<BaseParcelable> {
override fun createFromParcel(parcel: Parcel): BaseParcelable {
return BaseParcelable(parcel)
}
override fun newArray(size: Int): Array<BaseParcelable?> {
return arrayOfNulls(size)
}
}
}
然后用这个class在fragment之间或者(Activty to Fragment)之间传递数据
例如,要将列表从一个片段传递到另一个片段:
companion object {
fun newInstance(categoryId: Int, brandsList: ArrayList<Int>): Fragment {
val fragment = CategoryAllAdsFragment()
fragment.arguments = Bundle()
fragment.arguments!!.putInt(Constant.CATEGORY_ID, categoryId)
fragment.arguments!!.putParcelable(Constant.BRANDS_LIST, BaseParcelable(brandsList))
return fragment
}
}
获取列表:
val any = arguments?.getParcelable<BaseParcelable>(Constant.BRANDS_LIST).value
val list = any as ArrayList<Int>
我正在尝试将 ArrayList<Integer>
从片段传递到另一个片段,这是我的代码:
Kotlin 代码
companion object {
fun newInstance(categoryId: Int, brandsList: ArrayList<Int>): Fragment {
val fragment = CategoryAllAdsFragment()
fragment.arguments = Bundle()
fragment.arguments!!.putInt(Constant.CATEGORY_ID, categoryId)
fragment.arguments!!.putParcelableArrayList(Constant.BRANDS_LIST, brandsList)
return fragment
}
}
但上面写着:
Type mismatch.
Required: java.util.ArrayList !
Found: kotlin.collections.ArrayList /* = java.util.ArrayList */
当我试图阅读时也是如此。
Kotlin 代码
try {
val brandsList = arguments!!.getParcelableArrayList<Int>(Constant.BRANDS_LIST)
} catch (ex: Exception) {
throw Exception("brand list cannot be null")
}
它说:
Type argument is not within its bounds
Expected: Parcelable!
Found: Int
我用 Java
测试过它,它工作正常。
使用
putIntegerArrayList(String key, ArrayList value)
Inserts an ArrayList value into the mapping of this Bundle, replacing any existing value for the given key
putIntegerArrayList(Constant.BRANDS_LIST, array)
点赞
getIntegerArrayList(String key)
Returns the value associated with the given key, or null if no mapping of the desired type exists for the given key or a null value is explicitly associated with the key.
extras.getIntegerArrayList(Constant.BRANDS_LIST)
您可以创建一个通用的 Parcelable class,其中包含一个类型为 (Any) 的变量
class BaseParcelable : Parcelable {
var value: Any
constructor(value: Any) {
this.value = value
}
constructor(parcel: Parcel) {
this.value = Any()
}
override fun writeToParcel(dest: Parcel?, flags: Int) {}
override fun describeContents(): Int = 0
companion object CREATOR : Parcelable.Creator<BaseParcelable> {
override fun createFromParcel(parcel: Parcel): BaseParcelable {
return BaseParcelable(parcel)
}
override fun newArray(size: Int): Array<BaseParcelable?> {
return arrayOfNulls(size)
}
}
}
然后用这个class在fragment之间或者(Activty to Fragment)之间传递数据
例如,要将列表从一个片段传递到另一个片段:
companion object {
fun newInstance(categoryId: Int, brandsList: ArrayList<Int>): Fragment {
val fragment = CategoryAllAdsFragment()
fragment.arguments = Bundle()
fragment.arguments!!.putInt(Constant.CATEGORY_ID, categoryId)
fragment.arguments!!.putParcelable(Constant.BRANDS_LIST, BaseParcelable(brandsList))
return fragment
}
}
获取列表:
val any = arguments?.getParcelable<BaseParcelable>(Constant.BRANDS_LIST).value
val list = any as ArrayList<Int>