0

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;

    }
Hussain Aali
  • 49
  • 1
  • 9

1 Answers1

1

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);
    }
Gavriel
  • 18,880
  • 12
  • 68
  • 105
  • thank you sir, I tried this before and i checked again it showed this error,"" getSituation (String) in Situation cannot be applied to ()"" – Hussain Aali Feb 06 '16 at 18:54
  • I sorry by mistake I post the comment not completed, I edited agin – Hussain Aali Feb 06 '16 at 19:00
  • 1
    then fix the code in your question, because all I see is what you post there. According to the example you gave getSituation() should work. Or add more parts of the code by editing the question – Gavriel Feb 06 '16 at 19:04
  • but you don't have any getSituation() function in your code – Gavriel Feb 06 '16 at 19:23
  • the example just I made shortcut to make it easier, we can depend on status. "Processing" or "Completed", thanks – Hussain Aali Feb 06 '16 at 19:30
  • Look, i used the code you gave in the question, but you say it gives you an error that clearly states that there's no such function as getSituation() or that something is wrong with it. If you can't show the relevant code, then how can I help? – Gavriel Feb 06 '16 at 19:33
  • 1
    To get correct answer give forward some sensible question. You ask a question and then comment out of the world. how can one help you? this answer clearly answers your main question. either accept it or ask new question with new problem. SO is not for solving homework @HussainAali – Zahan Safallwa Feb 06 '16 at 19:36
  • I'm so sorry you are correct but I but set instead get again I did it it's worked, thanks allot – Hussain Aali Feb 06 '16 at 20:16