Compare names of every resource with the content of a variable of sqlite database

旧巷老猫 提交于 2019-11-29 04:37:14

Or in other words: How to get a list (containing Strings) of all resource names, preferential only drawable resources?

This is probably what you want:

Field[] fields = R.drawable.class.getFields();
String[] allDrawablesNames = new String[fields.length];
for (int  i =0; i < fields.length; i++) {           
    allDrawablesNames[i] = fields[i].getName();
}

Sorry, I know you were looking for a Java solution, but I did not find any solution for the C# and Xamarin issue in StackOverflow, so I will leave my solution for Android here if you don't mind.

I found a well written class ReflectionUtils in GitHub, just used it and made this method to call.

    private List<string> GetAllStrings()
    {
        IEnumerable<FieldInfo> fields = ReflectionUtils.GetAllFields(typeof(Resource.String));
        List<string> list = new List<string>();
        using (var enumerator = fields.GetEnumerator())
        {
            try
            {
                while (enumerator.MoveNext())
                {
                    list.Add(enumerator.Current.Name);
                }
            } catch (Exception e)
            {
                // Handle exception.
            }
        }
        return list;
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!