Last active
May 19, 2018 06:18
-
-
Save bassaer/87b681c353439f0112aec05f5585ec70 to your computer and use it in GitHub Desktop.
ネストしたデータをresourceで管理する ref: https://qiita.com/bassaer/items/b1d3f9bacb03033e3a46
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<resources> | |
<array name="menu"> | |
<item>@array/food_menu</item> | |
<item>@array/drink_menu</item> | |
</array> | |
<array name="food_menu"> | |
<item>food</item> | |
<item>@array/food_array</item> | |
</array> | |
<array name="drink_menu"> | |
<item>drink</item> | |
<item>@array/drink_array</item> | |
</array> | |
<string-array name="food_array"> | |
<item>Sandwich</item> | |
<item>Hot dog</item> | |
<item>Salad</item> | |
<item>Donuts</item> | |
</string-array> | |
<string-array name="drink_array"> | |
<item>Coffee</item> | |
<item>Tea</item> | |
<item>Coke</item> | |
<item>Water</item> | |
</string-array> | |
</resources> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"menu": { | |
"food": [ | |
"Samdwich", | |
"Hot dog", | |
"Salad", | |
"Donuts" | |
], | |
"drink": [ | |
"Coffee", | |
"Tea", | |
"Coke", | |
"Water" | |
] | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
05-19 05:44:01.973 5423-5423/com.github.bassaer.nestarrayapp D/Sample: food | |
05-19 05:44:01.973 5423-5423/com.github.bassaer.nestarrayapp D/Sample: └── Sandwich | |
05-19 05:44:01.973 5423-5423/com.github.bassaer.nestarrayapp D/Sample: └── Hot dog | |
05-19 05:44:01.973 5423-5423/com.github.bassaer.nestarrayapp D/Sample: └── Salad | |
05-19 05:44:01.973 5423-5423/com.github.bassaer.nestarrayapp D/Sample: └── Donuts | |
05-19 05:44:01.974 5423-5423/com.github.bassaer.nestarrayapp D/Sample: drink | |
05-19 05:44:01.974 5423-5423/com.github.bassaer.nestarrayapp D/Sample: └── Coffee | |
05-19 05:44:01.974 5423-5423/com.github.bassaer.nestarrayapp D/Sample: └── Tea | |
05-19 05:44:01.974 5423-5423/com.github.bassaer.nestarrayapp D/Sample: └── Coke | |
05-19 05:44:01.974 5423-5423/com.github.bassaer.nestarrayapp D/Sample: └── Water |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MainActivity : AppCompatActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
loadMenu() | |
} | |
private fun loadMenu() { | |
val tag = "Sample" | |
// <array name="menu">のデータを読み込む | |
val menus = resources.obtainTypedArray(R.array.menu) | |
// 0番目がメニューの名前で、1番目がメニュー配列のResourceID | |
@StyleableRes val nameIndex = 0 | |
@StyleableRes val arrayIndex = 1 | |
for (i in 0 until menus.length()) { | |
// @array/*_menu のResourceIdを取得 | |
val menuId = menus.getResourceId(i, -1) | |
if (menuId < 0) { | |
break | |
} | |
// @array/*_arrayを読み込む | |
val menu = resources.obtainTypedArray(menuId) | |
// 1番目のメニュー配列のResourceIDを取得 | |
val arrayId = menu.getResourceId(arrayIndex, -1) | |
if (arrayId < 0) { | |
menu.recycle() | |
break | |
} | |
// メニュー配列取得 | |
val array = resources.obtainTypedArray(arrayId) | |
// 出力 | |
Log.d(tag, menu.getString(nameIndex)) | |
for (j in 0 until array.length()) { | |
Log.d(tag, " └── " + array.getString(j)) | |
} | |
menu.recycle() | |
array.recycle() | |
} | |
menus.recycle() | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment