2010-11-16 21 views
8

Utworzono kolekcję ArrayList<HashMap<String, String>> w celu przechowywania moich danych dla ListView. Używam SimpleAdapter.Jak zrobić niestandardowy ListView z kolorowymi elementami tła?

Czy można zmienić tło elementu listy, gdy identyfikator elementu listy% 10 == 0?

Oto kod (metoda układ generujący):

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

Odpowiedz

8

zastąpić getView w karty, aby dokonać zmian w widoku. Pamiętaj, że ListView ponownie wykorzystuje implementacje widoku, więc jeśli zmienisz kolor na element 10, upewnij się, że kolor jest ustawiony przeciwnie dla wszystkich pozostałych widoków.

np.

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

Wielkie dzięki, ROZWIĄZANIA –

0

Aby to osiągnąć, trzeba stworzyć adapter zwyczaj tablicy, a następnie zmienić kolor tła, jeśli warunki są odpowiednie.

Wyjazd ten post na przykład: Custom ArrayAdapter setBackground in getView

+0

Wielkie dzięki, greate poradnik, ale użyłem tego poniżej –

3

Oto kod, mam nadzieję, że to będzie pomocne dla innych użytkowników

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); 
} 
Powiązane problemy