Home > Software engineering >  How to take an array from input in Android Kotlin
How to take an array from input in Android Kotlin

Time:11-13

I'm reasonably new to Kotlin and android as a whole. I'm trying to figure out a way to take input through an EditText and add it to an array by pressing a button to accept the values but I can't seem to figure it out. I have been trialing many options and nothing seems to work for me. Below I have pasted my current code. Any Help would be very appreciated because i'm stuck at the moment. Thanks in advance!

class MainActivity2 : AppCompatActivity() {
    private lateinit var addnumber: EditText
    private lateinit var storednumber: TextView
    private lateinit var output: TextView
    private lateinit var addbutton: Button
    private lateinit var clearbutton: Button
    private lateinit var averagebutton: Button
    private lateinit var minmaxbutton: Button


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main2)
        
        
        storednumber = findViewById(R.id.stored_tv)
        output = findViewById(R.id.answer2_tv)
        addbutton = findViewById(R.id.addNum_btn)
        clearbutton = findViewById(R.id.clear_btn)
        averagebutton = findViewById(R.id.average_btn)
        minmaxbutton = findViewById(R.id.minMax_btn)
        addbutton.setOnClickListener {

            val ed = findViewById<View>(R.id.et_addNum) as EditText
            var text = ed.text.toString()
            val arr =
                IntArray(text!!.length / 2) //Assuming no spaces and user is using one comma between numbers

            var i = 0
            while (text != null && text.length > 0) {
                arr[i] = text.substring(0, 1).toInt()
                text = text.substring(text.indexOf(",")   1)
                i  
            }

        }


    }
}

CodePudding user response:

I assume you're having trouble with actual String to Int conversion. I really suggest not to use the while logic and substrings in this situation since you're making your life complicated and Kotlin already provides simple way to transform data types one into another.

Collection transformation operations

fun main() {
    val textBox = "1,2,3"
    if (textBox != null && textBox.length > 0) {
        val intList = textBox.split(",").map { it.toInt() }
        print(intList)
    }
}

or if you want IntArray

fun main() {
    val textBox = "1,2,3"
    if (textBox != null && textBox.length > 0) {
        val intArray = textBox.split(",").map { it.toInt() }.toIntArray()
        print(intArray.contentToString())
    }
}

CodePudding user response:

It's very easy actually use below code:

       addbutton.setOnClickListener {

            val ed = findViewById<View>(R.id.et_addNum) as EditText
            var text = ed.text.toString()
            val array: List<String> = text.split(",")
            // That's is all you need. Now use this array where you want.
            // Example get 3rd item with array[2]
            // Or loop through it like below
            for (element in array) {
               Log.e("elements", element)
            }
        }
  • Related