0

I want to set the background color depending on the data for the row of the ListView. I implemented a ListActivity but I dont know how to get notified the load is completed so that I can access the rows of the ListView.

public class RouteList extends ListActivity {

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

    CommuteDb db = new CommuteDb(this);
    Cursor cursor = db.getRouteList();
    ListAdapter adapter = new SimpleCursorAdapter(this, // Context.
        R.layout.route_row, //row template
        cursor, // Pass in the cursor to bind to.
        new String[] { BaseColumns._ID, "Name" }, //Columns from table
        new int[] { R.id.id, R.id.name }, //View to display data
        0); //FLAG_REGISTER_CONTENT_OBSERVER

    setListAdapter(adapter);

    ListView listView = (ListView) findViewById(android.R.id.list);
    Log.i("RouteList","listView.getChildCount()=" + listView.getChildCount()); //returns always 0
//Loop will not execute because no row yet
    for (int i=0; i < listView.getChildCount(); i++) {
        View rowView = listView.getChildAt(i);
        Log.i("RouteList",rowView.getClass().getName());
        rowView.setBackgroundColor(0x88ff0000);
    }

If I execute this loop later (for instance on user's request) I am able to get each row and assign the color I want. However I need to do this automatically after data are loaded in ListView.

GuyOlivier
  • 183
  • 5
  • 13
  • 1
    you need to implement a custom CursorAdapter – Naveen Feb 19 '13 at 09:32
  • You can use custom adpaters in this case and in getview methord you can check the data and set teh background color accordingly – Triode Feb 19 '13 at 09:32
  • check this http://stackoverflow.com/questions/5300787/how-do-i-create-a-custom-cursor-adapter-for-a-listview-for-use-with-images-and-t – Naveen Feb 19 '13 at 09:35

1 Answers1

0

Thanx for the hint guys.
It works after modifying the code this way: I added a custom adapter (RouteAdapter) derived from SimpleCursorAdapter:

private class RouteAdapter extends SimpleCursorAdapter {

    public RouteAdapter(Context context, int layout, Cursor c,
            String[] from, int[] to, int flags) {
        super(context, layout, c, from, to, flags);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //Let the default getView do the job (inflate the row layout and set views to their values from database  
        View view = super.getView(position, convertView, parent);
        //Get the resulting view (route_row) and do custom loading
        TextView isOffer = (TextView) view.findViewById(R.id.isOffer);
        if (isOffer.getText().equals("0"))
                view.setBackgroundColor(0x8800ff00); //seek=green
            else
                view.setBackgroundColor(0x88ff0000); //offer=red

        return view;
    }
}

Then in my original code I just replaced SimpleCursorAdapter by RouteAdapter:

ListAdapter adapter = new RouteAdapter(this, // Context.
GuyOlivier
  • 183
  • 5
  • 13