How to do custom ListView with colorful items' backgrounds?
Asked Answered
B

3

8

I have created an ArrayList<HashMap<String, String>> collection to hold my data for ListView. I'm using SimpleAdapter.

Is it possible to change background of list item when list item's ID % 10 == 0?

Here is the code (method generating layout):

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);
}
Belford answered 16/11, 2010 at 22:40 Comment(0)
C
8

You override getView in your adapter to make changes to the view. Keep in mind that ListView reuses the view implementations, so if you change the color to item 10, make sure you set the color to the opposite for all other views.

e.g.

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;
  }
}
Cosma answered 16/11, 2010 at 23:38 Comment(0)
B
3

Here is the code, hope it'll be helpful for other users

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);
}
Belford answered 21/11, 2010 at 12:24 Comment(0)
O
0

To accomplish this, you need to create a custom array adapter and then change the background color if the conditions are right.

Check out this post for an example: Custom ArrayAdapter setBackground in getView

Ockeghem answered 16/11, 2010 at 23:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.