Search code examples
arrayskotlinarraylist

Making an array variable passable as an array in Kotlin from an intent to another activity


Basically, I have created an arrayList from the main activity class which goes like this:

 val qrResultList = ArrayList<String>()

Now, when I press the button, it shall add a value on the array list:

qrResultList.add(textResult.value)

Now, I want to press another button that actually puts the qrResultList as a full arraylistvariable.

                    val intent = Intent (this@MainActivity, QRSessionFullList::class.java)
                intent.putExtra("qrResultList", qrResultList) // push the whole qr result list to be hauled by the next activity.
                startActivity(intent) // pushes the event to the qr list

Now, I want to display it in a listview kind of thing, and then made a for loop to access it:

        val qrResultList = intent.getStringArrayExtra("qrResultList").toString() // coverts to array

    for (item in qrResultList) {
        val arrayAdapter:ArrayAdapter<String> = ArrayAdapter(
            this, android.R.layout.simple_list_item_1,
            qrResultList
        )
        qrlistview.adapter = arrayAdapter
        qrlistview.setOnItemClickListener {
                adapterView, view, i, l -> Toast.makeText(this, "Item Selected " + qrResultList[i],
            Toast.LENGTH_LONG).show()
        }
    }

But it says the ArrayAdapter can not be called with the events supplied. Is there a way I can assign the values because this part:

            val arrayAdapter:ArrayAdapter<String> = ArrayAdapter(
            this, android.R.layout.simple_list_item_1,
            qrResultList
        )

it does not accept the qrResultList as an array :(


Solution

  • When you use intent.putExtra() instead of intent.putStringArrayListExtra() you are encoding your ArrayList as a Serializable.

    When you retrieve an extra, you need to retrieve the same type you used to put in because it has to know how to decode it from however it was encoded when put in. So in this case, you could have retrieved it using getSerializableExtra("qrResultList") as ArrayList<String>, but that is a messy way to do it.

    The cleaner way to do it would be to use intent.putStringArrayListExtra(qrResultList), and then retrieving it using getStringArrayListExtra(). Note the difference from getStringArrayExtra(). ArrayLists and Arrays are two different things!

    As @dominicoder mentioned in the comments, you also cannot be using toString() on your result. This will convert your List (or null if you failed to correct the above) into a String, unusable for your ArrayAdapter.

    Since the returned list might be nullable, wrap your adapter creation code in an if (qrResultList != null) { }, or alternatively, you could call orEmpty() on it to convert it to non-nullable.

    Side note: It makes no sense to be creating your ArrayAdapter inside a for-loop that iterates the list. If the list has 10 items, then you are creating the ArrayAdapter 10 times and throwing away the first nine redundant copies.

    val qrResultList = intent.getStringArrayListExtra("qrResultList").orEmpty()
    
    val arrayAdapter:ArrayAdapter<String> = ArrayAdapter(
        this, android.R.layout.simple_list_item_1,
        qrResultList
    )
    qrlistview.adapter = arrayAdapter
    //...