मैं स्ट्रिंग या ड्रायरेबल जैसे संसाधनों को अपने नाम से एक्सेस करना चाहता हूं, न कि इसकी int आईडी।ज्ञात संसाधन नाम के साथ संसाधन आईडी कैसे प्राप्त करें?
इसके लिए मैं किस विधि का उपयोग करूंगा?
मैं स्ट्रिंग या ड्रायरेबल जैसे संसाधनों को अपने नाम से एक्सेस करना चाहता हूं, न कि इसकी int आईडी।ज्ञात संसाधन नाम के साथ संसाधन आईडी कैसे प्राप्त करें?
इसके लिए मैं किस विधि का उपयोग करूंगा?
कुछ की तरह यह हो जाएगा:
R.drawable.resourcename
सुनिश्चित करें कि आप Android.R
नाम स्थान आयातित के रूप में यह ग्रहण भ्रमित कर सकते हैं (अगर thats तुम क्या प्रयोग कर रहे हैं) न करना पड़े।
यदि वह काम नहीं करता है, तो आप हमेशा एक संदर्भ के getResources
विधि का उपयोग कर सकते हैं ...
Drawable resImg = this.context.getResources().getDrawable(R.drawable.resource);
कहाँ this.context
एक Activity
, Service
या किसी अन्य Context
उपवर्ग के रूप में intialised है।
अद्यतन:
यह नाम आप चाहते हैं, Resources
वर्ग (getResources()
द्वारा दिया) एक getResourceName(int)
विधि है रहा है, और एक getResourceTypeName(int)
?
अद्यतन 2:
public int getIdentifier (String name, String defType, String defPackage)
कौन सा निर्दिष्ट संसाधन नाम के पूर्णांक देता है, प्रकार & पैकेज:
Resources
वर्ग इस पद्धति है।
अगर मैं सही समझ में आया, यह तुम क्या चाहते
int drawableResourceId = this.getResources().getIdentifier("nameOfDrawable", "drawable", this.getPackageName());
कहाँ "इस" एक गतिविधि, लिखा बस स्पष्ट करने के लिए है।
मामले में आप strings.xml में एक स्ट्रिंग या एक यूआई तत्व, विकल्प "drawable"
int resourceId = this.getResources().getIdentifier("nameOfResource", "id", this.getPackageName());
मैं आपको चेतावनी देता का एक पहचानकर्ता चाहते हैं, पहचानकर्ता प्राप्त करने की इस तरह का उपयोग केवल जहां जरूरत वास्तव में धीमी है, ।
आधिकारिक दस्तावेज के लिए लिंक: Resources.getIdentifier(String name, String defType, String defPackage)
यह, आदि परीक्षण लेखन के संदर्भ यकीन है कि विशेष स्ट्रिंग से मौजूद बनाने के लिए या –
मैं आदमी नहीं जानता में बहुत उपयोगी है मैं की तरह काम किया टाइमइम्प के साथ getIdentifier() से पहले और बाद में एक लॉग जोड़ा है और यह मुझे दिखाता है कि यह 0 - 1 एमएस में निष्पादित हो जाता है! तो यह धीमा नहीं है, यह बहुत तेज़ है! मैं संसाधनों से छवियों को प्राप्त करने के लिए इसका उपयोग कर रहा हूं और यह पूरी तरह से काम करता है। नेक्सस 5x पर परीक्षण किया गया। –
मैं अपने विधि का उपयोग कर एक संसाधन आईडी प्राप्त करने के लिए आप सुझाव है। GetIdentidier() विधि का उपयोग करने से यह अधिक कुशल है, जो धीमा है।
/**
* @author Lonkly
* @param variableName - name of drawable, e.g R.drawable.<b>image</b>
* @param с - class of resource, e.g R.drawable.class or R.raw.class
* @return integer id of resource
*/
public static int getResId(String variableName, Class<?> с) {
Field field = null;
int resId = 0;
try {
field = с.getField(variableName);
try {
resId = field.getInt(null);
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
return resId;
}
यह सभी मामलों के लिए काम नहीं करेगा। उदाहरण के लिए यदि आपके पास
इसके अलावा आपकी विधि वास्तव में तेज़ नहीं है। चूंकि जावा क्लास क्रमबद्धता कभी भी काम नहीं कर रही है। – ddmytrenko
int resourceID =
this.getResources().getIdentifier("resource name", "resource type as mentioned in R.java",this.getPackageName());
यदि आपने पोस्ट किए गए कोड को समझाया तो यह बेहतर होगा। –
स्ट्रिंग से संसाधन आईडी प्राप्त करने के लिए एक आसान तरीका:
कोड यह रहा। यहां संसाधननाम संसाधन ImageView का नाम खींचने योग्य फ़ोल्डर में है जो एक्सएमएल फ़ाइल में भी शामिल है।
int resID = getResources().getIdentifier(resourceName, "id", getPackageName());
ImageView im = (ImageView) findViewById(resID);
Context context = im.getContext();
int id = context.getResources().getIdentifier(resourceName, "drawable",
context.getPackageName());
im.setImageResource(id);
मुझे this class संसाधनों के साथ संभालने में बहुत मददगार मिला है।यह कुछ परिभाषित किया गया है dimens, रंग, ड्रॉएबल और तार से निपटने के लिए विधियों, इस तरह:
public static String getString(Context context, String stringId) {
int sid = getStringId(context, stringId);
if (sid > 0) {
return context.getResources().getString(sid);
} else {
return "";
}
}
अलावा समाधान @lonkly को
विधि:
/**
* lookup a resource id by field name in static R.class
*
* @author - ceph3us
* @param variableName - name of drawable, e.g R.drawable.<b>image</b>
* @param с - class of resource, e.g R.drawable.class or R.raw.class
* @return integer id of resource
*/
public static int getResId(String variableName, Class<?> с)
throws android.content.res.Resources.NotFoundException {
try {
// lookup field in class
java.lang.reflect.Field field = с.getField(variableName);
// always set access when using reflections
// preventing IllegalAccessException
field.setAccessible(true);
// we can use here also Field.get() and do a cast
// receiver reference is null as it's static field
return field.getInt(null);
} catch (Exception e) {
// rethrow as not found ex
throw new Resources.NotFoundException(e.getMessage());
}
}
संभावित डुप्लिकेट [यदि मैं इसका नाम जानता हूं तो मुझे छवि की संसाधन आईडी कैसे मिल सकती है?] (Http://stackoverflow.com/questions/3042961/how-can-i-get-the-resource-id- की-एक छवि है, तो-ए-पता है कि इसके नाम) – ghoti