[Cotlin] After creating an empty array, it is not possible to add a value to the array each time the int value is exceeded.

Asked 2 years ago, Updated 2 years ago, 77 views

Hello, teachers. I'm asking you a question because there's something difficult about Cotlin.

What I want to do is enter a value in one activity and click the button It is added to the list view of other activities.

So we set the array used in the list view to blank, and the end value is Implemented the appearance of adding to an array when received.

However, it does not indicate that the array does not have a value or that the value of the array does not change.

I'm asking you a question because it's hard to solve this part, so I'd appreciate it if you could answer it.

(If you don't understand my question, please reply and I'll leave a comment right away.)

main activity
package com.sampleconst

import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import android.widget.ImageButton
import android.widget.ImageView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.*
import kotlinx.android.synthetic.main.activity_main.*


class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // Create an empty array
        var UserList = arrayListOf<User>()

        // Toast message
        val Adapter = UserAdapter(this, UserList)

        Checklist.adapter = Adapter

        Checklist.onItemClickListener = AdapterView.OnItemClickListener { parent, _, position, _ ->
            val selectItem = parent.getItemAtPosition(position) as User
            Toast.makeText(this, selectItem.name, Toast.LENGTH_SHORT).show()
        }


        // Switch Activity
        btn_a.setOnClickListener{
            valentent = Intent (this, Addlist::class.java) // Create an object to display the next screen
            startActivity(intent)
        }

        // Adding values from other activities to an empty array
        if(intent.hasExtra("msg")){
            val Additem01 = intent.getStringExtra("msg")
            UserList.plus (User(R.drawable.play, Additem01.toString(), "Example 1", "Example 2"))

        }
    }
}
Customized class in array
package com.sampleconst

class User (val profile: Int, val name: String, val age: String, val greet: String)

kotlin android

2022-09-20 19:50

1 Answers

It is confirmed that the value cannot be modified in the array you mentioned.I think is correct.

Plus also returns a new array with elements added, but does not change the actual data.

So you can use add or use it as below.

UserList = UserList.plus (User(R.drawable.play, Additem01.toString(), "Example 1", "Example 2")

public operator fun <T> Collection<T>.plus(element: T): List<T> {
    val result = ArrayList<T>(size + 1)
    result.addAll(this)
    result.add(element)
    return result
}

https://stackoverflow.com/questions/57770663/difference-between-plus-vs-add-in-kotlin-list

Please refer to the above information.

Thank you.


2022-09-20 19:50

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.