首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >getParcelableArrayListExtra导致将不同的类型设置为变量。

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

Stack Overflow用户
提问于 2022-08-04 08:22:24
回答 1查看 241关注 0票数 0

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

一个用户类。

代码语言:javascript
复制
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变量的随机类。

代码语言:javascript
复制
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

填充意图的类

代码语言:javascript
复制
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)
  }   
}

测试类

代码语言:javascript
复制
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
    ) {
        mUserList = getParcelableArrayListExtra(EXTRA_PAYMENT_OPTIONS_EXTRA)
      }
    }
  }
}

类型检查(as,as?)与预期的getParcelable函数一起工作。但是,当涉及到getParcelableArrayListExtra时,它就是不能工作,并给出编译错误,正如我前面解释的那样。

您知道as, as?检查的最佳选择是什么吗?以及mUserList如何能够接受不同类型的数组并假装喜欢它呢?

EN

回答 1

Stack Overflow用户

发布于 2022-08-08 14:34:01

这是一个混乱的原因如下:

您正在用Kotlin编写代码的

  • ,但您所处理的类(ParcelableBundleIntentArrayList)实际上是Java中的Java
  • 泛型,是一个黑客

我会把问题分成两部分:

ArrayList<Parcelable>

  • Check/convert
  1. ArrayList封装到ArrayList<Parcelable>中,将ArrayList<Parcelable>内容合并为预期的

类型

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73232619

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档