/**
* Converts 3 resource arrays to ArrayList<SelectableColor> of colors. Colors can be sorted by name at runtime, note that colors will be sorted in language displays to user.
* Note: all arrays must have equal lengths.
*
* @param context current context
* @param sortByName if true colors will be sorted by name, otherwise colors will be left as they are
* @param idsArray array resource id to use as colors ids
* @param namesArray array resource id to use as colors names
* @param colorsArray array resource id to use as colors, well color values
* @return colors ArrayList
*/
public static ArrayList<SelectableColor> convertResourceArraysToColorsArrayList(Context context, boolean sortByName, @ArrayRes int idsArray, @ArrayRes int namesArray, @ArrayRes int colorsArray) {
//get and check arrays
String[] ids = context.getResources().getStringArray(idsArray);
int[] colors = context.getResources().getIntArray(colorsArray);
String[] names = context.getResources().getStringArray(namesArray);
if (ids.length != colors.length && ids.length != names.length) {
Log.e(LOG_TAG, "convertResourceArraysToColorsArrayList(): Arrays must have equals lengths!");
return null;
}
//create ArrayList
ArrayList<SelectableColor> result = new ArrayList<>();
for (int i = 0; i < ids.length; i++) {
result.add(new SelectableColor(ids[i], names[i], colors[i]));
}
//sort by names
if (sortByName) {
Collections.sort(result, new SelectableItemNameComparator<SelectableColor>());
}
return result;
}
Utils.java 文件源码
java
阅读 39
收藏 0
点赞 0
评论 0
项目:SelectionDialogs
作者:
评论列表
文章目录