change ListView row colors based on TextView in item

1.4k views Asked by At

Evening Everyone,

making a simple budget app and want a red background of the row when it is a cost and a green background when it is an income. Have the listview working and everything, now just delving into the colors. Already figured out changing row colors for on click but now looking to have an "initial" coloring of the rows. (this is my first app so still very much learning)

I have tried - custom SimpleAdapter wrapper:

public class RC_Adapter extends SimpleAdapter {
Context contextpass;

public RC_Adapter(Context context, List<? extends Map<String, ?>> data,
        int resource, String[] from, int[] to) {
    super(context, data, resource, from, to);
    contextpass = context;
    // TODO Auto-generated constructor stub
}
@Override
public View getView(int position,View convertView,ViewGroup parent){
    TextView tv = (TextView) convertView.findViewById(R.id.c1);
    String s = tv.getText().toString();
    if(s.equals("Cost")){
        convertView.setBackgroundResource(R.color.valencia_red);}
    else{
        convertView.setBackgroundResource(R.color.t_green);
    }
    return convertView; 
}
}

Implemented with:

RC_Adapter sd=new RC_Adapter(this,arrCosts,R.layout.costlist,
                         new String[]{"id","desc","cost"},
                         new int[]{R.id.c1,R.id.c2,R.id.c3});

I have also tried:

SimpleAdapter.ViewBinder binder = new SimpleAdapter.ViewBinder() {

        @Override
        public boolean setViewValue(View view, Object data,
                String textRepresentation) {
            // TODO Auto-generated method stub
            TextView tv = (TextView) view.findViewById(R.id.c1);
            String s = tv.getText().toString();
            if(s.equals("Cost")){
                view.setBackgroundResource(R.color.valencia_red);}
            else{
                view.setBackgroundResource(R.color.t_green);
            }
            return false;
        }
    };
    sd.setViewBinder(binder);

Both just crash the program as soon as I call the functions that initiate the changed simpleadapters. I have not tried calling both at once. I tried the extended class and then I tried the binder. Are there obvious mistakes in the code? I have been trying to adapt multiple different things to get it to work (like Change color of an item in ListView with SimpleAdapter)

Thanks for any help! Always appreciated

1

There are 1 answers

5
JRomero On BEST ANSWER

Without looking at the logs I can assume you are getting a NullPointerException. convertView would be null since you need to create a view first by calling super.getView. Once you've got the view (let parent inflate or convert) you can alter it.

@Override
public View getView(int position,View convertView,ViewGroup parent){
    View view = super.getView(position, convertView, parent);
    TextView tv = (TextView) view.findViewById(R.id.c1);
    // TODO: Add null checks
    String s = tv.getText().toString();
    if(s.equals("Cost")){
        view.setBackgroundResource(R.color.valencia_red);}
    else{
        view.setBackgroundResource(R.color.t_green);
    }
    return view; 
}