如何更改项目的背景颜色由一个选项
问题描述:
空调列表视图我有我的代码:如何更改项目的背景颜色由一个选项
AdapterView.OnItemClickListener listener = new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String elementoSeleccionado = parent.getItemAtPosition(position).toString();
Boolean realizado = getRealizadoPorElemento(elementoSeleccionado);
if (realizado == true){
actualizaRealizado(elementoSeleccionado,"N");
elementosList.getChildAt(position).setBackgroundColor(Color.TRANSPARENT);
} else if (realizado == false){
actualizaRealizado(elementoSeleccionado,"S");
elementosList.getChildAt(position).setBackgroundColor(Color.parseColor("#3DF400"));
}
}
};
这仍然工作,但问题就在这里。
我有一个方法运行在列表视图和更新一些与其他颜色:
ArrayList<String> elementos = LeerElementosLista();
if (elementos.isEmpty() == false) {
ArrayAdapter arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, LeerElementosLista());
elementosList.setAdapter(arrayAdapter);
Boolean realizadoelem = false;
int count = elementosList.getCount();
for (int i = 0; i < count; i++){
String elem = elementosList.getItemAtPosition(i).toString();
realizadoelem = getRealizadoPorElemento(elem);
if (realizadoelem == true){
elementosList.getChildAt(i).setBackgroundColor(Color.parseColor("#3DF400"));
}
}
} else {
//Toast.makeText(this, "No hay Listas para mostrar!!!", Toast.LENGTH_SHORT);
}
而且仅此行的第二个方法
elementosList.getChildAt(i).setBackgroundColor(Color.parseColor("#3DF400"));
不工作,在应用程序关闭,但我在控制台中没有错误。
答
也许你需要设置绘制/选择器而不是颜色。如果你重写默认背景,它会删除默认选择器。因此你的列表是用一个选择器创建的,你试图改变它。也许这里是冲突。
但是更好提供logcat输出。它会更容易;
答
那不是这样做的方式。
您需要从BaseAdapter类创建自定义适配器。
在那里,您可以在getView()方法上设置单元格的背景颜色。
希望这会有所帮助。
+0
如何在自定义适配器中实现OnItemClickLisatener和OnLongItemClickListener? –
+0
与arrayadapter完全相同的方式,监听器不是用于适配器的列表 – Nanoc
提供logcat输出 –