Read values from arraylist

Hello everyone,

ive got a problem to read values from an arraylist which is stored in another array list. my code to store the data works, but i can’t read it.

my code to store the data:

            allheater_ON_OFF.allMembers.forEach[item | 
            val itemdata = newArrayList()
            logInfo("heater.rules", "1")
            itemdata.add(item)
            itemdata.add(item.state.toString)
            oldstateheater.add(itemdata) 
       ]

to read the items in the array “itemdata” i tried:

oldstateheater.get(0).get(0)

but i got the error:

‘get’ is not a member of ‘java.lang.Object’;

the reason behind this is to loop trought the oldstateheater array and send the old state to the stored item.

Thanks for help

Jens

The .get(int) method returns a object. You have to cast it to a Arraylist.
I think you can try it with this (not tested):

val ArrayList data = oldstateheater.get(0)
data.get(0)
1 Like

I’m looking at this code and can’t help but think it’s an XY Problem.

Some additional problems you may encounter is that the Item Object you get for a given Item on one run of the Rule may not be the same Object that you get for the same Object later. As a result, the Item you’ve stored in your ArrayList may become disconnected from the event bus so your sendCommand or postUpdate calls may not work. It’s better to store the Item’s name in this case instead of the Item itself. When using a Map, it’s critical to not use the Item itself as the key.

  • Why are you storing the Item and the String representation of it’s state to an ArrayList? Especially when you already have a list of those Items in a Group?

because i need the state at the time the rule was performed

  • Why are you using an ArrayList instead of a HashMap using the Item name as the key?

i tried also hashmaps but got the same problem :smiley:

i didn’t know this funktions :smiley:

but overall i fixed my funktion with the info from the first comment:

    var data = newArrayList()
    data = oldstateheater.get(i)
    logInfo("heater.rules", data.get(0))

but for now i will change my code to use the new functions

great :ok_hand:

hello again,

now i changed my code to:

    var Map states
    states=storeStates(allheater_ON_OFF)
    restoreStates(states)

how is it possible to restore only ONE of the itemsstates in the MAP “states”?

Thanks for help again :slight_smile:

states is a Map. It uses the Item as the key so the following might work.

MyItem.postUpdate(states.get(MyItem))

If it doesn’t work, you’ll need to filter the keys for the one with the name of the Item you want. See Design Pattern: Working with Groups in Rules.

val key = states.keySet.filter[ i | i.name == "MyItem"]
MyItem.postUpdate(states.get(key))