我的项目的drawable文件夹中有大量资源,所有这些资源都显示在GridView
中,现在当用户从GridView
中选择图像时,图像应该加载并显示在屏幕上。问题是我的课程变得非常大,只需要做一个“if”导向的编程来知道点击了哪个图像。我相信有一种更有效的方法可以使用HashMap
或类似的方式加载资源。
这里只是一个菜单的一个小例子:
secondList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg20, View arg21, int arg22, long arg23) {
clearbackground2();
arg21.setBackgroundResource(R.drawable.fondoselected);
((TextView) arg21.findViewById(R.id.textoItem)).setTextColor(Color.parseColor("#8fb5e3"));
if (arg22 == 0) {
final int menuSecondSelected = arg22;
txtThirdTitleList.setText("Curvas");
thirdList.setAdapter(new SketchMenuSideAdapter((Activity) view.getContext(), getResources().getStringArray(R.array.viaCurva), getResources().obtainTypedArray(R.array.viaCurvaIcon), 3));
thirdList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> argTrh20, View argTrh21, int arg22, long arg2Trh3) {
if (arg22 == 0) {
addNewImage(menuSecondSelected, "1carril", null, null);
}
if (arg22 == 1) {
addNewImage(menuSecondSelected, "2carriles", null, null);
}
if (arg22 == 2) {
addNewImage(menuSecondSelected, "3carriles", null, null);
}
if (arg22 == 3) {
addNewImage(menuSecondSelected, "abierta1", null, null);
}
if (arg22 == 4) {
addNewImage(menuSecondSelected, "abierta2", null, null);
}
if (arg22 == 5) {
addNewImage(menuSecondSelected, "abierta3", null, null);
}
if (arg22 == 6) {
addNewImage(menuSecondSelected, "abierta4", null, null);
}
if (arg22 == 7) {
addNewImage(menuSecondSelected, "abierta5", null, null);
}
if (arg22 == 8) {
addNewImage(menuSecondSelected, "cerrada1", null, null);
}
if (arg22 == 9) {
addNewImage(menuSecondSelected, "cerrada2", null, null);
}
if (arg22 == 10) {
addNewImage(menuSecondSelected, "cerrada3", null, null);
}
if (arg22 == 11) {
addNewImage(menuSecondSelected, "cerrada4", null, null);
}
if (arg22 == 12) {
addNewImage(menuSecondSelected, "cerrada5", null, null);
}
if (arg22 == 13) {
addNewImage(menuSecondSelected, "carril1", null, null);
}
if (arg22 == 14) {
addNewImage(menuSecondSelected, "carril2", null, null);
}
if (arg22 == 15) {
addNewImage(menuSecondSelected, "carril3", null, null);
}
if (arg22 == 16) {
addNewImage(menuSecondSelected, "carril4", null, null);
}
if (arg22 == 17) {
addNewImage(menuSecondSelected, "carril5", null, null);
}
if (arg22 == 18) {
addNewImage(menuSecondSelected, "carril6", null, null);
}
}
});
}
大约有6000行只有这种菜单,有人可以提出更好的方法吗?
答案 0 :(得分:2)
我建议使用SparseArray。它是Android平台中的一个集合,它将整数映射到对象。在功能上它等于Map<Integer, Object>
。 HashMap(稍微)更快,但SparseArray针对内存使用进行了优化。
这提高了可读性,因为您只需定义并填充一次地图,然后只用一种方法获得相应的值。
SparseArray<String> menuMap = new SparseArray<>();
menuMap.put(0, "1carril");
menuMap.put(1, "2carril");
menuMap.put(2, "3carril");
menuMap.put(3, "abierta1");
menuMap.put(4, "abierta2");
menuMap.put(5, "abierta3");
menuMap.put(6, "abierta4");
menuMap.put(7, "abierta5");
menuMap.put(8, "carril1");
menuMap.put(9, "carril2");
menuMap.put(10, "carril3");
然后只需在需要的地方获取映射值:
@Override
public void onItemClick(AdapterView<?> argTrh20, View argTrh21, int arg22, long arg2Trh3) {
String value = menuMap.get(arg22);
if(value != null) {
addNewImage(menuSecondSelected, value, null, null);
}
}
如果您有足够的物品,这将是合适的。这将极大地提高您需要值的可读性。您仍然必须首先将所有对添加到地图中,但这可以完成(即使在其他类中)。您还可以更轻松地更改或添加新值。
在效率方面:你至少应该使用if-else或switch语句。除此之外它不会变化那么多,如果有足够的物品,它甚至可能偏向于地图。但这不应该是一个真正的问题。可读性和可维护性比微小的效率差异更重要。
编辑:它实际上可以更短。我现在才意识到arg22
只是列表中的位置
首先,适当地命名您的参数。 argTrh20
,argTrh21
和arg22
是可怕的名字
但是,因为位置总是在0和某个值之间,所以你可以只使用一个数组。您不需要将int映射到值,因为它可以只是数组中的位置
现在,您可以用更少的行来定义它,并减少不必要的代码:
String[] menuValues = {"1carril", "2carril", "3carril", "rest here", "..." };
现在将该值减少为:
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(position < menuValues.length) { // this is just a defensive check. If you are sure the array is the same length as the max amount of items, this can be omitted.
addNewImage(menuSecondSelected, menuValues[position], null, null);
}
}
希望有所帮助
答案 1 :(得分:0)
对于Performance,您可以使用开关而不是if
为了便于阅读,所以你的主要课程不是那么大
为此创建一个新类:
主:
thirdList.setOnItemClickListener(new clicked())
OnItemClicked类:
public Class clicked implements AdapterView.OnItemClickListener{
public void onItemClick(AdapterView<?> argTrh20, View argTrh21, int arg22, long arg2Trh3) {
if (arg22 == 0) {
addNewImage(menuSecondSelected, "1carril", null, null);
}
if (arg22 == 1) {
addNewImage(menuSecondSelected, "2carriles", null, null);
}
if (arg22 == 2) {
addNewImage(menuSecondSelected, "3carriles", null, null);
}
if (arg22 == 3) {
addNewImage(menuSecondSelected, "abierta1", null, null);
}
if (arg22 == 4) {
addNewImage(menuSecondSelected, "abierta2", null, null);
}
if (arg22 == 5) {
addNewImage(menuSecondSelected, "abierta3", null, null);
}
if (arg22 == 6) {
addNewImage(menuSecondSelected, "abierta4", null, null);
}
if (arg22 == 7) {
addNewImage(menuSecondSelected, "abierta5", null, null);
}
if (arg22 == 8) {
addNewImage(menuSecondSelected, "cerrada1", null, null);
}
if (arg22 == 9) {
addNewImage(menuSecondSelected, "cerrada2", null, null);
}
if (arg22 == 10) {
addNewImage(menuSecondSelected, "cerrada3", null, null);
}
if (arg22 == 11) {
addNewImage(menuSecondSelected, "cerrada4", null, null);
}
if (arg22 == 12) {
addNewImage(menuSecondSelected, "cerrada5", null, null);
}
if (arg22 == 13) {
addNewImage(menuSecondSelected, "carril1", null, null);
}
if (arg22 == 14) {
addNewImage(menuSecondSelected, "carril2", null, null);
}
if (arg22 == 15) {
addNewImage(menuSecondSelected, "carril3", null, null);
}
if (arg22 == 16) {
addNewImage(menuSecondSelected, "carril4", null, null);
}
if (arg22 == 17) {
addNewImage(menuSecondSelected, "carril5", null, null);
}
if (arg22 == 18) {
addNewImage(menuSecondSelected, "carril6", null, null);
}
}
}