2015-11-03 235 views
0

空調列表視圖我有我的代碼:如何更改項目的背景顏色由一個選項

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")); 

不工作,在應用程序關閉,但我在控制檯中沒有錯誤。

+1

提供logcat輸出 –

回答

0

也許你需要設置繪製/選擇器而不是顏色。如果你重寫默認背景,它會刪除默認選擇器。因此你的列表是用一個選擇器創建的,你試圖改變它。也許這裏是衝突。

但是更好提供logcat輸出。它會更容易;

1

那不是這樣做的方式。

您需要從BaseAdapter類創建自定義適配器。

在那裏,您可以在getView()方法上設置單元格的背景顏色。

希望這會有所幫助。

+0

如何在自定義適配器中實現OnItemClickLisatener和OnLongItemClickListener? –

+0

與arrayadapter完全相同的方式,監聽器不是用於適配器的列表 – Nanoc

相關問題