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

2

6

I hope you can help me, I already researched my case but didn't found a good answer. I want to compare the content of a variable with the names of all existing resources (if possible only with drawable resources).

The question in short: How to compare the String content of an variable with a list of all resource names, preferential only with drawable resources? Or in other words: How to get a list (containing Strings) of all resource names, preferential only drawable resources?

The Case: I want to display a symbol based on a given type. This type is retrieved from a SQLite Database. There are many symbols stored as a drawable resource, they all are named like on of the possible types. For every stored type in the database I want to display the fitting symbol in a list. The equality should be find out through a comparison (via the contains method) between the variable "type" and a list containing the names of all (drawable) resources.

Example: In the database are types named "A", "B" and "C". In the drawable resources folder are graphics with the name "A", "BX" and "S". The comparison should lead to the case, that in the list only type "A" is connected with the fitting drawable symbol "A". Type "B" and "C" have no likely named drawable resources and thus shouldn't display any symbol.

I hope you understood my question and thank you in advanced for your help.

Starstarboard answered 11/12, 2012 at 12:40 Comment(4)
What have you already done? Because this is a list of requirements, not a question.Dosimeter
I already did a longer research in stackoverflow and generally via google. And this is not a list of requirements, this is one question how to do the comparison. I hope after editing it now sounds more like a question to you.Starstarboard
In that case I may have simply missed your question. Do me a favor and highlight it, preferably by ending it with a question mark.Dosimeter
edited again and highlighted questionStarstarboard
G
17

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();
}
Guardroom answered 11/12, 2012 at 13:20 Comment(2)
Thanks a lot, this is what i was looking for, I just changed the String[] to List<String> so that I can easily use the contains() method for the comparison.Starstarboard
This returns keys...how to get values?Blase
A
1

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;
    }
Administer answered 19/2, 2018 at 11:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.