androidjsonlistviewconvertview

setBackgroundColor (ListView) depend on the data


I'm going to create a dynamic ListView that display data from server with json. I want to make a setBakgroundColor depend on some object in the data. for example: json is

{"Order":[{"id":1,
"situation":"notchecked",
"status":"Processing"},
{"id":2,
"situation":"checked",
"status":"Processing"}]}

if situation == notchecked

convertView.setBackgroundColor(Color.GREEN);

this is my View in BaseAdapter

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.complete_order_row, parent,
                    false);
                if ()......{

                convertView.setBackgroundColor(Color.GREEN);
                 }
        }


        TextView situation = (TextView) convertView
                .findViewById(R.id.situation);
        situation.setText(catList.get(position).getSituation());
         TextView status= (TextView) convertView
                .findViewById(R.id.status);
        status.setText(catList.get(position).getStatus());
         TextView id= (TextView) convertView
                .findViewById(R.id.id);
        id.setText(catList.get(position).getId));

        return convertView;

    }

Solution

  • You almost got it right, but you need to set it every time, both when convertView is recycled and when it's not:

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.complete_order_row, parent,
                    false);
           //...
        }
        TextView situation = (TextView) convertView
                .findViewById(R.id.situation);
        situation.setText(catList.get(position).getSituation());
        if (catList.get(position).getSituation().equals("notchecked")) {
           convertView.setBackgroundColor(Color.GREEN);
        } else {
           convertView.setBackgroundColor(Color.BLUE);
        }