Tuesday, July 5, 2011

Android List view with alternating row colors

In this article, we shall see how to apply alternating row colors to a listview.

Steps:
  • Create your own Adapter and override getView method.
  • Use your own logic and set background color inside the getview method.
Source Code


/**
*
*/
package com.blogs.adapters;

import android.content.Context;
import android.database.Cursor;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SimpleCursorAdapter;

/**
* @author C'dine
* A simple cursor adapter. Only variation is that it displays alternate rows
* in alternate colors.
*/
public class AlternateRowCursorAdapter extends SimpleCursorAdapter{

private int[] colors = new int[] { Color.parseColor("#F0F0F0"), Color.parseColor("#D2E4FC") };
//private int[] colors = new int[] { R., 0x30808080 };
public AlternateRowCursorAdapter(Context context, int layout, Cursor c,
String[] from, int[] to) {
super(context, layout, c, from, to);
}
/**
* Display rows in alternating colors
*/
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
int colorPos = position % colors.length;
view.setBackgroundColor(colors[colorPos]);
return view;
}
}


Output:

6 comments:

  1. How to use this without extends SimpleCursorAdapter

    ReplyDelete
    Replies
    1. public class MainActivity extends Activity {

      @Override
      protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

      // Get reference of widgets from XML layout
      final ListView lv = (ListView) findViewById(R.id.lv);

      // Initializing a new String Array
      String[] fruits = new String[] {
      "Limequat",
      "Kwai Muk",
      "Mountain pepper",
      "Mora de Castilla",
      "Mountain Soursop"
      };

      // Create a List from String Array elements
      final List fruits_list = new ArrayList(Arrays.asList(fruits));

      // Create an ArrayAdapter from List
      final ArrayAdapter arrayAdapter = new ArrayAdapter
      (this, android.R.layout.simple_list_item_1, fruits_list){
      @Override
      public View getView(int position, View convertView, ViewGroup parent){
      // Get the current item from ListView
      View view = super.getView(position,convertView,parent);
      if(position %2 == 1)
      {
      // Set a background color for ListView regular row/item
      view.setBackgroundColor(Color.parseColor("#FFB6B546"));
      }
      else
      {
      // Set the background color for alternate row/item
      view.setBackgroundColor(Color.parseColor("#FFCCCB4C"));
      }
      return view;
      }
      };

      // DataBind ListView with items from ArrayAdapter
      lv.setAdapter(arrayAdapter);
      }
      }

      Delete
  2. public View getView (int position, View contextView, ViewGroup parent){
    View view = super.getView(position, contextView, parent);
    inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if(contextView == null){
    contextView = inflater.inflate(R.layout.model,parent,false);
    }
    TextView gridviewtv = (TextView) contextView.findViewById(R.id.gridviewtv);
    gridviewtv.setText(IndicatorName[position]);

    if (position % 2 == 1) {
    view.setBackgroundColor(Color.BLUE);
    } else {
    view.setBackgroundColor(Color.CYAN);
    }

    return contextView;
    }

    in my code this line "View view = super.getView(position, contextView, parent);"
    gives a compiler error "Abstract method getView can not be accessed directly"

    ReplyDelete