0
一個字符串數組我收到字符串數組的名字從我的意圖演員(KI1,KI2等)建設資源位置在android系統
而不是硬編碼字符串數組名稱(如R.array.KI1),我希望變量替換它們。
strdata = receiverIntent.getExtras().getString("intentExtra");
String[] pointDetails = getResources().getStringArray**(R.array.KI1);**
資源文件
<resources>
<string-array name="KI1">
<item>Categories Text KI1</item>
<item>Unitary Channel Text KI1</item>
<item>Localization Text KI1</item>
<item>Action Text KI1</item>
<item>Indication Text KI1</item>
<item>Point location Text KI1</item>
<item>KI1.png</item>
</string-array>
<string-array name="KI2">
<item>Categories Text KI2</item>
<item>Unitary Channel Text KI2</item>
<item>Localization Text KI2</item>
<item>Action Text KI2</item>
<item>Indication Text KI2</item>
<item>Point location Text KI2</item>
<item>KI2.png</item>
</string-array>
</resources>
爲什麼不可能做這樣的事情:
String[] pointDetails = getResources().getStringArray("R.array." + strdata);
我想要一個變量的輸出是這樣的:R.array.strdata
如果有KI400,我會寫400案件,但如果我能知道如何用一個變量替換這將是2行代碼。 – woeiwkq
爲了實現你想要的,你需要通過名稱而不是id來獲取資源。這是如何: 'Resources res = getResources(); int id = res.getIdentifier(strdata,「array」,getContext()。getPackageName()); String [] pointDetails = res.getStringArray(id);' – George
感謝@Giorgos,您的解決方案能夠運作:) – woeiwkq