Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android:按名称获取自定义xml类对象的资源ID_Android_Android Xml_Android Resources - Fatal编程技术网

Android:按名称获取自定义xml类对象的资源ID

Android:按名称获取自定义xml类对象的资源ID,android,android-xml,android-resources,Android,Android Xml,Android Resources,我的“res”文件夹中有一个自定义文件夹、文件和自定义XML资源类 我创建了一些自定义对象,我称之为: <area id="@+id/someId" name="Some Name" /> 这可能吗 我试过使用: int i = this.getResources().getIdentifier("Some Name", "area", this.getPackageName()); ……但这似乎不起作用。我总是得到0分 编辑 正如Geobits在下面所建议的,是否有一种方法可以

我的“res”文件夹中有一个自定义文件夹、文件和自定义XML资源类

我创建了一些自定义对象,我称之为:

<area id="@+id/someId" name="Some Name" />
这可能吗

我试过使用:

int i = this.getResources().getIdentifier("Some Name", "area", this.getPackageName());
……但这似乎不起作用。我总是得到0分

编辑

正如Geobits在下面所建议的,是否有一种方法可以从res文件加载所有资源并将它们保存在数组/映射中,例如
map
,以便我以后可以搜索它们

谢谢你的帮助

尝试使用

int resID=getResources().getIdentifier("name", "id", getPackageName());  
要获取资源的ID。

请尝试以下操作:

int i = this.getResources().getIdentifier("someId", "id", this.getPackageName());

它想要的
defType
就是它的标识符的形式。因为它是
R.id.someId
,所以您需要
id
。如果它是
R.drawable.someDrawable
,你会使用
drawable

,我不确定这是否是你需要的。但这是我的解决方案。如果您的资源是可提取的,我会这样做:

    public int findResourceIdByName(String name) {
        Field[] fields = R.drawable.class.getFields();  // get all drawables
        try {
            for(int i=0; i<fields.length; i++) {        // loop through all drawable resources in R.drawable
                int curResId = fields[i].getInt(R.drawable.class); // Returns the value of the field in the specified object as an int.
                                                                  //This reproduces the effect of object.fieldName

                Drawable drawable = getResources().getDrawable(R.drawable.icon); // get the Drawable object
                if(drawable.getName().equals(name)) {   //getName() is NOT possible for drawable, this is just an example
                    return curResId;                    // return the corresponding resourceId
                                                        // or you could return the drawable object instead, 
                                                        // depending on what you need.
                }
            }

            return -1; // no ResourceId found for this name

        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

至少。

我试过:
int itemId=this.getResources().getIdentifier(“某个名称”,“id”,this.getPackageName())
我仍然得到itemId==0。不要使用
somename
,使用
someId
。但在这一点上,我只知道用户单击了“somename”,我想找到与“somename”关联的id。好的,我明白了。你用
someId
的代码把我扔到了你的OP里。我认为您可能需要保留一个自定义对象的列表,并在代码中按名称手动搜索它们。嗯,对不起。我希望有一个更简单的方法来做到这一点。用xml解析它?我最终遵循了这一思路。我从XML文档中加载了所有名称,并通过修改此方法将它们与ID匹配。这段代码给我id名称和id int,从xml加载我有区域名称和区域id名称,这样我可以将int id与区域名称匹配。name id_name id_nameint_idI很高兴这对您有所帮助,尽管它是为可绘制资源编写的。我只是不确定我必须使用哪种自定义资源,这就是为什么我选择了drawable。
    public int findResourceIdByName(String name) {
        Field[] fields = R.drawable.class.getFields();  // get all drawables
        try {
            for(int i=0; i<fields.length; i++) {        // loop through all drawable resources in R.drawable
                int curResId = fields[i].getInt(R.drawable.class); // Returns the value of the field in the specified object as an int.
                                                                  //This reproduces the effect of object.fieldName

                Drawable drawable = getResources().getDrawable(R.drawable.icon); // get the Drawable object
                if(drawable.getName().equals(name)) {   //getName() is NOT possible for drawable, this is just an example
                    return curResId;                    // return the corresponding resourceId
                                                        // or you could return the drawable object instead, 
                                                        // depending on what you need.
                }
            }

            return -1; // no ResourceId found for this name

        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
Field[] fields = R.drawable.class.getFields();