【问题标题】:Android: Get Resource ID of custom xml class object by nameAndroid:按名称获取自定义xml类对象的资源ID
【发布时间】:2012-10-08 10:22:16
【问题描述】:

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

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

<area id="@+id/someId" name="Some Name" />

我可以通过 R.id.someId 静态访问它们。

但是,我需要在运行时获取资源 ID,并且需要通过“名称”来完成。换句话说,我在列表中显示“某个名称”,并且我需要获取知道用户从 ListView 中选择了“某个名称”的 id。 (我不是在寻找 ListItem 的 id,我实际上是想搜索我的资源并获取区域 xml 对象的 id)

例如:

我想做以下事情:

int id = getIdFromResourceName("Some Name"); 

这可能吗?

我尝试过使用:

int i = this.getResources().getIdentifier("Some Name", "area", this.getPackageName());

...但这似乎不起作用。我总是得到 0。

编辑

正如 Geobits 下面所建议的,有没有办法从 res 文件中加载所有资源并将它们保存在数组/地图中,例如 Map&lt;id,name&gt;,以便我以后可以搜索它们?

感谢您的帮助!

【问题讨论】:

    标签: android android-xml android-resources


    【解决方案1】:

    我不确定这是否是您需要的。但这是我的解决方案建议。如果您的资源是可绘制的,我会这样做:

        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();
    

    至少。

    【讨论】:

    • 我最终遵循了这个思路。我从我的 XML 文档中加载了所有名称,并使用此方法的修改将它们与 ID 匹配。这段代码给了我 id 名称和 id int,从 xml 加载我有区域名称和区域 id 名称,这样我可以将 int id 与区域名称匹配。 name id_name id_nameint_id
    • 很高兴这对您有所帮助,即使它是为可绘制资源编写的。我只是不确定我必须使用哪种自定义资源,这就是我选择 drawable 的原因。
    【解决方案2】:

    试试这个:

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

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

    【讨论】:

    • 我试过了:int itemId = this.getResources().getIdentifier("Some Name", "id", this.getPackageName()); 我仍然得到 itemId == 0。
    • 不要使用Some Name,使用someId
    • 但是那时,我只知道用户点击了“Some Name”,我想找到与“Some Name”关联的id。
    • 好的,我明白了。你用someId 的代码把我扔到了你的OP 中。我认为您可能需要保留自定义对象的列表并在代码中手动按名称搜索它们。
    • 嗯,对不起。我希望有一个更简单的方法来做到这一点。从 xml 中解析出来?
    【解决方案3】:

    尝试使用,

    int resID=getResources().getIdentifier("name", "id", getPackageName());  
    

    获取资源的ID。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-28
      • 2011-03-29
      相关资源
      最近更新 更多