在我的项目中,我使用以下代码:
List<Map<String,?>> data = new ArrayList<>();
data.add(ImmutableMap.of("icon", R.drawable.icon1, "label", getString(R.string.title1)));
data.add(ImmutableMap.of("icon", R.drawable.icon2, "label", getString(R.string.title2)));
data.add(ImmutableMap.of("icon", R.drawable.icon3, "label", getString(R.string.title3)));
data.add(ImmutableMap.of("icon", R.drawable.icon4, "label", getString(R.string.title4)));
data.add(ImmutableMap.of("icon", R.drawable.icon5, "label", getString(R.string.title5)));
我想知道是否有类似的方法可以不使用guava ImmutableMap,因为我需要从依赖项中删除它。
答案 0 :(得分:1)
是的,定义一个类并创建该类的ArrayList
。
示例:强>
public class DataHolder {
// declare the variables String, int, String, String
public DataHolder(String str1, int drawableID, String str2, String str3){
//initialize the class variables with the values passed in constructor
}
}
然后创建一个ArrayList<DataHolder> ad
并将对象添加到其中:
ad.add(new DataHolder("icon", R.drawable.icon1, "label", getString(R.string.title1));
ad.add(new DataHolder("icon", R.drawable.icon2, "label", getString(R.string.title2));
ad.add(new DataHolder("icon", R.drawable.icon3, "label", getString(R.string.title3));
ad.add(new DataHolder("icon", R.drawable.icon4, "label", getString(R.string.title4));