0

I have listview with custom base adapter. When I populate the list I have to check a boolean in the object I'm populating the listview and if it is true to change the background color of that row.

public View getView(int position, View convertView, ViewGroup parent) {
    LoginsList entry = listOfLoginsLists.get(position);
    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.lists_row, null);
    }

    TextView ListName = (TextView) convertView.findViewById(R.id.tvListName);
    ListName.setText(entry.getListName());

    TextView ListDescription = (TextView) convertView.findViewById(R.id.tvListDescription);
    ListDescription.setText(entry.getListDescription());

    Button Send = (Button) convertView.findViewById(R.id.bSend);
    Send.setOnClickListener(this);
    Send.setTag(entry);

    RelativeLayout RelLayout = (RelativeLayout) convertView.findViewById(R.id.layoutListsRow);
    RelLayout.setFocusableInTouchMode(false);
    RelLayout.setFocusable(false);
    RelLayout.setOnClickListener(this);
    RelLayout.setTag(entry);

    if (entry.isSent()) {
        RelLayout.setBackgroundColor(Color.parseColor("#4400FF00"));
    }

    return convertView;
}

But this code doesn't work right. When I scroll the list view the rows colors get messed up.

nikmin
  • 1,803
  • 3
  • 28
  • 46

2 Answers2

3
if (entry.isSent()) {
        RelLayout.setBackgroundColor(Color.parseColor("#4400FF00"));
}else {
        RelLayout.setBackgroundColor(//default color);
}
Tomislav Novoselec
  • 4,570
  • 2
  • 27
  • 22
  • And you need the else part because without the else part, the background colour of any view that was set to `"#4400FF00"` will never go back to what it was. – Shivam Verma Jul 12 '14 at 02:28
0

may be have have defined list selector .... or else use

RelLayout.setBackgroundResource(R.color.mycolor);

Also check whether it in any case your isSent() condtion is true or not;

Vipin Sahu
  • 1,441
  • 1
  • 18
  • 29