Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
129 views
in Technique[技术] by (71.8m points)

java - How can I plus objects in an array in Android?

So I'm trying to do a calorie counter. I have an array with many food items and another array with the values of each food item (just numbers in order as the food).

I have 3 Spinners too that shows and allows the user to choose a food in every spinner, and then there is a plain textview that will show the resulting sum of the all calories for each food we've selected. The only thing I need to know is how can I increment this number.

This is the 2 arrays that I got:

    <resources>
    <string name="app_name">calorias</string>
    <array name="comida">
        <item>Seleccione</item>
        <item>tortilla</item>
        <item>patatas fritas</item>
        <item>arroz</item>
        <item>salmon</item>
        <item>pollo</item>
        <item>pasta</item>
        <item>ensalada</item>
        <item>atun</item>
        <item>salchichas</item>
        <item>espinacas</item>
    </array>

    <array name="calorias">
        <item>0</item>
        <item>500</item>
        <item>674</item>
        <item>1170</item>
        <item>508</item>
        <item>199</item>
        <item>270</item>
        <item>98</item>
        <item>234</item>
        <item>1670</item>
        <item>310</item>
    </array>
</resources>

I don't know how can I do it.

question from:https://stackoverflow.com/questions/65848176/how-can-i-plus-objects-in-an-array-in-android

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

From my understanding, you cannot modify Android xml resources dynamically. Something like this is better stored in a database. You can look at Android's Room to be able to save those values and then modify them.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...