Search code examples
androidlistviewsimplecursoradaptercustom-lists

Change background colour of listview items based on cursor results


I have the following ListView..

private void populateKPIListView(String storedStaffId, View view, String id){
    //List 1
    Cursor cursor = db.getAllRows("*", dbTables.TABLE_KPIS, "WHERE projectid=" + id);

    int count = db.getRowCount("*", dbTables.TABLE_KPIS, "WHERE projectid=" + id);

    TextView empty = (TextView)view.findViewById(android.R.id.empty);
    if(count > 0){
        empty.setVisibility(View.GONE);
        String[] fromFieldNames = new String[] {dbTables.KEY_KPIHEADER, dbTables.KEY_TARGET, dbTables.KEY_ACTUAL};
        int[] toViewIDs  = new int[] {          R.id.card_title,        R.id.card_target,    R.id.card_actual};
        myCustomAdaptor = new CustomListAdapter(getActivity(), R.layout.kpi_list_item_card, cursor, fromFieldNames, toViewIDs, 0);
        kpiList = (ListView)view.findViewById(android.R.id.list);
        kpiList.setAdapter(myCustomAdaptor);

        kpiList.setOnItemClickListener(new OnItemClickListener(){

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent i = new Intent(getActivity(), KpiPopUpActivity.class);
                 i.putExtra("DB_ID", id);
                 startActivity(i);

            }

        });
    }else{
        empty.setVisibility(View.VISIBLE);
        empty.setText(R.string.kpi_empty_string);
    }
}

What I need to do is change the colour of a an individual list item if the 'target' is lower than the 'actual' in the cursor. I created a simple custom list adapter...

public class CustomListAdapter extends SimpleCursorAdapter {

private int mSelectedPosition;
Cursor items;
private Context context;
private int layout;

public CustomListAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int num) {
    super(context, layout, c, from, to, num);
    this.context = context;
    this.layout = layout;
} 

@Override
public View getView(int position, View convertView, ViewGroup parent) {
     View v = super.getView(position, convertView, parent);
     Cursor c = getCursor();
     c.moveToPosition(position);
     int target = c.getColumnIndex(dbTables.KEY_TARGET);
     int actual = c.getColumnIndex(dbTables.KEY_ACTUAL);
     RelativeLayout relLay = (RelativeLayout)v.findViewById(R.id.kpi_box);
     Log.i(""+target, actual+"");
     if (actual > target) {
         // Set the background color of the text.

         relLay.setBackgroundColor(v.getResources().getColor(R.color.urgent));
     } else {
         relLay.setBackgroundColor(v.getResources().getColor(R.color.white));
     }
     return v;
  }

}

The problem is that this changes all list items to red. The Log produces the same produces the same result for every item (hence them all being red). I says that every item has a target of 7 and an actual of 8. In reality I know that none of the results have actuals or targets with these numbers.


Solution

  • The problem is, you're getting the column index and not the value in the column in that row. Try the following:

    int target = c.getInt(c.getColumnIndex(dbTables.KEY_TARGET));
    int actual = c.getInt(c.getColumnIndex(dbTables.KEY_ACTUAL));