Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I pass a mutable list to a bundle?

I want to add a mutable list to a bundle, but there doesn't seem to be a way to accomplish this.

var bundle = Bundle()
                bundle.put...????("LIST", fbModel.recipeArray)

You can use putString and so on, but there doesn't seem to be a putMutableList as an option. What to do?

UPDATE Forgot to mention that the mutableList recipeArray is an object. Like this:

var recipeArray: MutableList<RecipeTemplate>

...where RecipeTemplate is a class that looks like this:

class RecipeTemplate {
    var recipeHeader: String? = null
    var recipeText: String? = null
    var recipeImage: String? = null
    var recipeKey: String? = null
}

UPDATE Solved issue according to answer by @EpicPandaForce:

gridView.setOnItemClickListener { parent, view, position, id ->
                Log.d("TAGA", "CLICKETICLICK " + position)
                Log.d("TAGA", "CLICKETICLICK " + fbModel.recipeArray[1].recipeHeader) //PASSES

                val intent = Intent(classContext, Recipes::class.java)

                var bundle = Bundle().apply {
                    putParcelableArrayList("LIST", ArrayList<Parcelable>(fbModel.recipeArray))
                    putInt("POSITION", position)
                }

                intent.putExtra("bundle", bundle)
                startActivity(intent)
            }

But I'm still having problem receiving it in the other activity. Code here from onCreate:

var passedIntent = intent.extras
var bundle: Bundle = passedIntent.getBundle("bundle")
var counter: Int = bundle.getInt("POSITION", 0)
var recipeArray: ArrayList<Parcelable> = bundle.getParcelableArrayList("LIST")
var recipeList: MutableList<RecipeTemplate> = recipeArray as MutableList<RecipeTemplate>

Log.d("TAGA", "PASSED " + counter) //PASSES
if(recipeArray != null) {
    Log.d("TAGA", "HERE " + recipeArray[1].recipeHeader.toString()) //Null
    Log.d("TAGA", "HERE " + recipeList[1].recipeHeader.toString()) //Null
    Log.d("TAGA", "HERE " + recipeArray.size) //PASSES

}

The counter is passed and the correct number is shown. The recipeArray.size is passed and shows the correct number. However, the other logs recipeArray[1].recipeHeader.toString() and recipeList[1].recipeHeader.toString() are both null even though they contain the correct values before being put in the bundle. Is there something I need to do to... eum... de-parse the list? Or am I missing something?

like image 578
tore Avatar asked Jul 02 '18 07:07

tore


People also ask

How do I add a list to a mutable list?

To add an element to a Mutable List in Kotlin, we can use add(element), or add(index, element) functions. add(element) adds element to the end of this Mutable List. add(index, element) adds element to this Mutable List at the given index.

What is the difference between list and mutable list?

A quick glance at Kotlin's documentation provides a good overview about what the difference between them is: while List provides read-only access to stored elements, MutableList provides “list-specific write operations” that allow us to add or remove particular elements from an existing list.

How do you pass a list on Kotlin?

But lately in Kotlin all you have to do is simple. First, in your data class you implement Serializable. Then in your source Activity, you add it to your bundle and cast it as Serializable. In your destination Activity, you retrieved the list.

What is the difference between list and mutable list in Kotlin?

These are some important points you should know before working with Kotlin MutableList: List is read-only (immutable), you cannot add or update items in the original list. MutableList inherites List and supports read/write access, you can add, update or remove items.


2 Answers

You can do

//apply plugin: 'kotlin-android-extensions' 

//androidExtensions {
//    experimental = true
//}

apply plugin: 'kotlin-parcelize'

Then

@Parcelize
class RecipeTemplate : Parcelable {
    var recipeHeader: String? = null
    var recipeText: String? = null
    var recipeImage: String? = null
    var recipeKey: String? = null
}

Then

var bundle = Bundle().apply {
                  putParcelableArrayList("LIST", ArrayList<Parcelable>(fbModel.recipeArray))
             }
like image 167
EpicPandaForce Avatar answered Sep 21 '22 18:09

EpicPandaForce


This depends on what items you have in your list. Bundle has a couple methods that allow you to place lists in it, for example putIntegerArrayList and putStringArrayList. If you need to use a custom type, you can make it Parcelable and use putParcelableArrayList.

Of course, all these methods as their name suggest take ArrayList instances specifically, not just any MutableList. So you can either change all your MutableList usages to use ArrayList instead, or you can create a new ArrayList from your existing MutableList when you're putting it in the bundle:

bundle.putParcelableArrayList("key", ArrayList(myParcelableList))
like image 22
zsmb13 Avatar answered Sep 20 '22 18:09

zsmb13