Java tutorial
//package com.java2s; /* * Copyright 2017 Gurupad Mamadapur * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import android.annotation.SuppressLint; import android.content.Context; import android.content.res.TypedArray; import android.support.annotation.ArrayRes; import android.util.Pair; import java.util.HashMap; import java.util.Map; public class Main { @SuppressLint("UseSparseArrays") @SuppressWarnings("ResourceType") public static Map<Integer, Pair<String, String>> obtainBadgeMap(Context context, @ArrayRes int id) { TypedArray badgeArray = context.getResources().obtainTypedArray(id); Map<Integer, Pair<String, String>> badgeMap = new HashMap<>(); for (int i = 0; i < badgeArray.length(); i++) { int resId = badgeArray.getResourceId(i, -1); if (resId != -1) { TypedArray array = context.getResources().obtainTypedArray(resId); badgeMap.put(resId, new Pair<>(array.getString(0), array.getString(1))); array.recycle(); } } badgeArray.recycle(); return badgeMap; } }