Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

193
Views
Android Livedata updates his states with copy of objects

Hi i think i don't really now how live data works.

I am having a 2D Array with prefilled Values.

val randomboard =
    arrayOf(arrayOf(0, 0, 0, 0), arrayOf(0, 0, 0, 0), arrayOf(0, 0, 0, 0), arrayOf(0, 0, 0, 0))

And a Live Data Object that posts just once at init Function of my ViewModel the current State of my Array.

init {
    _preFillButtons.postValue(randomboard.copyOf())
}

So i want that only the current State of my Array and no future states are pushed to my LiveData Objects so i gave it a copy of my Array.

When i am changing any value of my Array randomboard and then change like my orientation my livedata object has just this new Value in his state without that i pushed any new State to that LiveData object.

Maybe Livedata object is not that what i need there but i don't know why even the copy object is updated.

UPDATE:

The complete Code

class GameActivity : AppCompatActivity(), View.OnClickListener {

val viewModel: GameViewModel by viewModels()

val buttonIDs = arrayOf(
    intArrayOf(R.id.button1, R.id.button2, R.id.button3, R.id.button4),
    intArrayOf(R.id.button5, R.id.button6, R.id.button7, R.id.button8),
    intArrayOf(R.id.button9, R.id.button10, R.id.button11, R.id.button12),
    intArrayOf(R.id.button13, R.id.button14, R.id.button15, R.id.button16)
)

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

    viewModel.preFillButtons.observe(this) {
        prefillButtons(it)
    }
}

private fun prefillButtons(board: Array<Array<Int>>) {
    for (row in 0..3) {
        for (col in 0..3) {
            val button = findViewById<Button>(buttonIDs[row][col])
            button.setTag(R.id.row, row)
            button.setTag(R.id.column, col)
            button.setOnClickListener(this)
            if (board[row][col] != 0) {
                button.text = board[row][col].toString()
                button.isEnabled = false
            } else {
                button.text = getString(R.string.defaultbuttontext)
                button.isEnabled = true
            }
        }
    }
}

override fun onClick(view: View?) {
    val button = view as? Button
    if (button != null) {
        val row = button.getTag(R.id.row) as Int
        val col = button.getTag(R.id.column) as Int
        showAlertDialog(col, row)
    }
}

private fun showAlertDialog(col: Int, row: Int) {
    val builder = AlertDialog.Builder(this)
    builder.setItems(R.array.choices) { dialogInterface: DialogInterface, i: Int ->
        if (i != 4) {
            viewModel.trySetValue(row, col, i + 1)
        }
    }
    builder.setTitle(getString(R.string.dialogtitle, row + 1, col + 1))
    builder.show()
}
}

ViewModel:

class GameViewModel : ViewModel() {

val preFillButtons: LiveData<Array<Array<Int>>>
    get() = _preFillButtons
private val _preFillButtons = MutableLiveData<Array<Array<Int>>>()

val randomboard =
    arrayOf(arrayOf(0, 0, 0, 0), arrayOf(0, 0, 0, 0), arrayOf(0, 0, 0, 0), arrayOf(0, 0, 0, 0))

init {
    _preFillButtons.postValue(randomboard.copyOf())
}

fun trySetValue(row: Int, col: Int, num: Int) {
    randomboard[0][0] = 1
}
}
over 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!