相关文章推荐
还单身的烈马  ·  React 新手入門 - 學習 Web ...·  1 年前    · 
绅士的闹钟  ·  SQLite 联接表操作 | ...·  2 年前    · 
无聊的莴苣  ·  定时任务 Cron 表达式,了解一下? - 掘金·  3 年前    · 
坚强的甘蔗  ·  (数学)P、NP、NPC、NP ...·  3 年前    · 
完美的长颈鹿  ·  UVC摄像头 java 控制库 java ...·  3 年前    · 
Code  ›  getParcelableArrayListExtra导致将不同的类型设置为变量。开发者社区
arraylist 变量
https://cloud.tencent.com/developer/ask/sof/107182440
非常酷的可乐
2 年前
首页
学习
活动
专区
工具
TVP 最新优惠活动
返回腾讯云官网
提问

问 getParcelableArrayListExtra导致将不同的类型设置为变量。

Stack Overflow用户
提问于 2022-08-04 08:22:24
EN

问题从 getParcelableArrayListExtra 开始,当我们尝试将它设置为变量时,它不支持类型检查。让我举一个尽可能基本的例子。

一个用户类。

import kotlinx.parcelize.Parcelize
import android.os.Parcelable
    @Parcelize
    data class UserClass(
            var name: String? = null,
            var text: String? = null,
            var age: Int? = null
    ) : Parcelable

我们将尝试设置为User变量的随机类。

import android.os.Parcelable
import kotlinx.parcelize.Parcelize
@Parcelize
data class MessageClass(
    val title: String?, = Constant.STRING_EMPTY
    val text: String? = Constant.STRING_EMPTY
) : Parcelable

填充意图的类

class FillIntentClass(){
    //Let's say one of the developers added the MessageClass object inside our intent.
    //Or BE sent the wrong type of object and I passed its value to the intent.
    private fun DummyFunctionToSetIntent(){
    val messageList = arraylistOf(MessageClass(title = "hello",text ="dummy text")
    intent.putParcelableArrayListExtra(EXTRA_PAYMENT_OPTIONS_EXTRA, messageList)
}

测试类

class MyTestClass(){
  // UserList variable 
   private var mUserList: ArrayList<UserClass>? = null
   override fun onCreate(savedInstanceState: Bundle?) {
     with(intent) {
     // In this situation, mUserList became the type of ArrayList<MessageClass>
     // But it shouldn't be possible. Because it must accept only ArrayList<UserClass>
     // And that causes mostly crashes when the other code parts use it.
     mUserList = getParcelableArrayListExtra(EXTRA_PAYMENT_OPTIONS_EXTRA)
     // mUserList now pretend its like ArrayList<MessageClass>. But i set it as ArrayList<UserClass> at the top of the class.
     // The best way to solve this is to type check with as?. If the type is not as expected it must return null.
     // But I cannot use type check here. It gives me a "Not enough information to infer type variable T" error.
     mUserList = getParcelableArrayListExtra(EXTRA_PAYMENT_OPTIONS_EXTRA) as? ArrayList<UserClass> //(compile error here on IDE)
     // So I had to come out with the below solution. But I cannot say it's the best practice.
     if (getParcelableArrayListExtra<UserClass>(EXTRA_PAYMENT_OPTIONS_EXTRA)
            ?.filterIsInstance<UserClass>()?.isNotEmpty() == true
 
推荐文章
还单身的烈马  ·  React 新手入門 - 學習 Web 開發 | MDN
1 年前
绅士的闹钟  ·  SQLite 联接表操作 | 学习笔记-阿里云开发者社区
2 年前
无聊的莴苣  ·  定时任务 Cron 表达式,了解一下? - 掘金
3 年前
坚强的甘蔗  ·  (数学)P、NP、NPC、NP hard问题 - AI大道理 - 博客园
3 年前
完美的长颈鹿  ·  UVC摄像头 java 控制库 java opencv 摄像头_mob6454cc7945bd的技术博客_51CTO博客
3 年前
今天看啥   ·   Py中国   ·   codingpro   ·   小百科   ·   link之家   ·   卧龙AI搜索
删除内容请联系邮箱 2879853325@qq.com
Code - 代码工具平台
© 2024 ~ 沪ICP备11025650号