Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
176 views
in Technique[技术] by (71.8m points)

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...