Home » Android » android – Is there a better way than using 3 different arrays to display 3 types of data-Exceptionshub

android – Is there a better way than using 3 different arrays to display 3 types of data-Exceptionshub

Posted by: admin February 26, 2020 Leave a comment

Questions:

I’m making a soundboard app from an example I found, and each sound has an id, the text shown, and a picture.

The sample uses 3 arrays defined in arrays.xml to do that, an array for the text, an array for the id of the sound (@raw/example) and an array for the picture (@mipmap/ic_example).

An object is created with the value of the 3 arrays :

 val res = context.applicationContext.resources
    val labels = res.obtainTypedArray(R.array.labels)
    val ids = res.obtainTypedArray(R.array.ids)
    val pictureIds = res.obtainTypedArray(R.array.pictureIds)
    val sounds = ArrayList<Sound>()
    for (i in 0 until labels.length()) {
        sounds.add(
            Sound(
                labels.getString(i),
                ids.getResourceId(i, -1),
                pictureIds.getResourceId(i, -1)
            )
        )
    }

I started to fill the arrays but it started to be difficult to link the values

Is there a way to regoup those 3 arrays into one ?

How to&Answers: