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
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
–
–
–
–
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.
–
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.