添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
强悍的红烧肉  ·  Vba ...·  2 小时前    · 
深情的路灯  ·  VB.Net ArrayList - 菜鸟教程·  6 天前    · 
绅士的水煮肉  ·  Java ArrayList ...·  6 天前    · 
飘逸的冰淇淋  ·  python contains ...·  2 月前    · 
酷酷的黄花菜  ·  Is your constructor ...·  4 月前    · 
满身肌肉的火锅  ·  MapPropertySource ...·  6 月前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Kotlin Android: How to pass ArrayList of Serializable objects through intent without unchecked cast warning?

Ask Question
public void showThings(ArrayList<Thing> things) {
    Intent intent = new Intent(this, ThingActivity.class);
    intent.putExtra(THINGS, things);
    startActivity(intent);

Then in the ThingActivity I want to get the ArrayList<Thing>

class ThingActivity {
   var things: ArrayList<Thing>? = null
   override fun onCreate(savedInstanceState: Bundle?) {
       things = intent.extras.getSerializable(OtherActivity.THINGS) as? ArrayList<Thing>

Unfortunately, I can't seem to figure out how to cast to the appropriate type without triggering an "unchecked cast" warning. Is there a way to gracefully set to null if (somehow unexpectedly) the cast fails?

Appending ?: return null does not seem to work as I've seen suggested elsewhere

In IntelliJ or Android Studio, Alt+Enter, left-arrow on the item, suppress for the method, or class, or some other level. Hides the warning – Zoe stands with Ukraine Oct 12, 2018 at 16:26 Unfortunately, there's no way with Bundle. This library makes use of generics and implements type-safe data passing between Fragments: github.com/Miha-x64/Flawless – Miha_x64 Oct 12, 2018 at 16:32 Ideally I'd pass whatever test it's expecting me to pass, not a big fan of supressing warnings. – ray Oct 12, 2018 at 16:34 Do you have access to the source of Thing ? You could implement Parcelable on it and then use the parcelable-related methods of Intent instead of the serializable-related ones. – Ben P. Oct 12, 2018 at 16:49

The unchecked cast warning is happening due to the way Java generics work at runtime. Because of type erasure, at runtime, the type of the list is just List, and not List<Thing>. That means that the cast is considered unsafe, even though it's quite possible for a human to look at the code and see that there's no problem.

While I agree with you that suppressing the warning isn't ideal, in this case I think it's fine.

The best solution, though, would be to implement the Parcelable interface on Thing. That way, when you want to pass a List<Thing> through an intent, you could write:

intent.putParcelableArrayListExtra(THINGS, things)

And when you want to read it back out:

things = intent.extras.getParcelableArrayListExtra(OtherActivity.THINGS)

Neither of these will cause a compiler warning.

This is good info. I'll read up more about Parcelable it seems that it's recommended for passing custom objects through Intents anyway. – ray Oct 12, 2018 at 18:28

As an alternative to Ben P's answer, you could use Gson.

Assuming that Things is simply a data class (holds a bunch of variables), this will work perfectly (this is also required by Ben P's answer).

Here's a way to implement it:

public void showThings(ArrayList<Thing> things) {
    String json = new Gson().toJson(things);
    Intent intent = new Intent(this, ThingActivity.class);
    intent.putExtra(THINGS, json);
    startActivity(intent);

Then you can get the ArrayList like this:

String json = intent.getStringExtra(THINGS);
TypeToken<ArrayList<Things>> token = new TypeToken<ArrayList<Things>>() {};
ArrayList<Things> things = new Gson().fromJson(json, token.getType());
val intent = Intent(this, SecondActivity::class.java)
val arrayGuide = ArrayList<Guide>()
intent.putParcelableArrayListExtra("arrayInfo",arrayGuide)
startActivity(intent)

Activity two:

if(intent != null){
  val arrayList = 
  this.intent.getParcelableArrayListExtra<Guide>("arrayInfo")
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.