2010-11-16 7 views
8

He creado una colección ArrayList<HashMap<String, String>> para guardar mis datos para ListView. Estoy usando SimpleAdapter.¿Cómo hacer una vista de lista personalizada con fondos de elementos coloridos?

¿Es posible cambiar el fondo del elemento de la lista cuando la ID del elemento de la lista% 10 == 0?

Aquí está el código (disposición de generación de método):

private void fillData() { 

    Cursor c = this.mDbManager.getNgOrderDetailByOrderNumber(this.mNumber); 

    ArrayList<HashMap<String, String>> items = new ArrayList<HashMap<String, String>>(); 

    if (!c.isAfterLast()) { 
     do { 
      // ... filling HashMap and putting it to ArrayList 
     } while (c.moveToNext()); 
    } 

    SimpleAdapter adapter = new SimpleAdapter(this, items, R.layout.list_item, 
     new String[] { "product", "ordered", "price", "discount" }, 
     new int[] { R.id.ProductTextView, R.id.OrderedTextView, 
     R.id.PriceTextView, R.id.DiscountTextView }); 
    ListView l = (ListView) findViewById(android.R.id.list); 
    l.setAdapter(adapter); 
} 

Respuesta

8

Puede alterar temporalmente getView en su adaptador para hacer cambios a la vista. Tenga en cuenta que ListView reutiliza las implementaciones de vista, por lo que si cambia el color al elemento 10, asegúrese de establecer el color en el opuesto para todas las demás vistas.

p. Ej.

new SimpleAdapter(...) { 
    @Override 
    public View getView (int position, View convertView, ViewGroup parent) { 
    View view = super.getView(position, convertView, parent); 
    if (position == 10) { 
     // set background color = red; 
    } else { 
     // set background color = green; 
    } 
    return view; 
    } 
} 
+0

muchas gracias, SOLUCIONADO –

0

Para lograr esto, es necesario crear un adaptador de matriz personalizada y cambie el color de fondo si las condiciones son adecuadas.

Salida este post para ver un ejemplo: Custom ArrayAdapter setBackground in getView

+0

muchas gracias, greate tutorial, pero he utilizado la de abajo –

3

Aquí está el código, espero que sea útil para otros usuarios

private void fillData() { 

    Cursor c = this.mDbManager.getNgOrderDetailByOrderNumber(this.mNumber); 

    ArrayList < HashMap < String, String >> items = new ArrayList < HashMap < String, String >>(); 

    if (!c.isAfterLast()) { 
     do { 
      // ... filling HashMap and putting it to ArrayList 
     } while (c.moveToNext()); 
    } 

    SimpleAdapter adapter = new SimpleAdapter(this, items, R.layout.list_item, 
     new String[] { 
     "product", "ordered", "price", "discount" 
    }, 
     new int[] { 
     R.id.ProductTextView, R.id.OrderedTextView, 
     R.id.PriceTextView, R.id.DiscountTextView 
    }) { 

     // here is the method you need to override, to achieve colorful list 

     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 

      View view = super.getView(position, convertView, parent); 

      HashMap < String, String > items = (HashMap < String, String >) getListView() 
       .getItemAtPosition(position); 
      if (Long.parseLong(items.get("id")) % 10 == 0) { 
       view.setBackgroundColor(Color.GREEN); 
      } else { 
       view.setBackgroundColor(Color.YELLOW); 
      } 
      return view; 
     } 

    }; 
    ListView l = (ListView) findViewById(android.R.id.list); 
    l.setAdapter(adapter); 
} 
Cuestiones relacionadas