كيفية الحصول على Android String Array إذا كان لدي اسم Array في سلسلة أخرى

StackOverflow https://stackoverflow.com//questions/11685678

سؤال

لدي مشكلة، مشكلة فنية.لدي عرض قائمة، عندما أنقر فوق عنصر ما، أحصل على محتوى في هذا الموضع المحدد وأرسله إلى النشاط التالي من خلال "putExtra"

في النشاط التالي ما أفعله هو

        String item;
        Bundle extras = getIntent().getExtras(); 
        if (extras != null) {
            item = extras.getString("item");
        }

الآن دعنا نقول هنا العنصر = "جافا"؛

الآن ما أريده هو termArray = getResources().getStringArray(R.array.Java);

لكنني لا أريد R.array.Java، أريد شيئًا مثل R.array.غرض

مصفوفات السلسلة في XML

<string-array name="Java">
<item>A</item>
<item>B</item>
<item>C</item>
</string-array>

<string-array name="C">
<item>A</item>
<item>B</item>
<item>C</item>
</string-array>

<string-array name="php">
<item>A</item>
<item>B</item>
<item>C</item>
</string-array>

يمكنني القيام بهذه المهمة باستخدام IF-else ولكن لدي العديد من مصفوفات السلسلة، لذا لا يمكن استخدام IF-else

هل كانت مفيدة؟

المحلول

حاول كـ:

 String item;
        Bundle extras = getIntent().getExtras(); 
        if (extras != null) {
            item = extras.getString("item");
            int arryid = this.getResources().getIdentifier(item, "array",
                this.getPackageName()); 
            termsArray  = this.getResources().getStringArray(arryid);
        }
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top