Phil
Phil

Reputation: 993

ArrayAdapter getView position is not true

For my ListView I have this ArrayAdapter

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, android.R.id.text1, uas) {
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View item = super.getView(position, convertView, parent);

            if(uas.size() > 0 && tinydb.getInt("selectedUA", 0) == position) {
                item.setBackgroundColor(Color.GREEN);
            }

            return item;
        }
    };

Lets say the selectedUA is 3, so it colors the 3rd index green. The problem is that the first index always gets colored too. It seems that the position value or something has a bug. Can anyone help me?

Upvotes: 1

Views: 263

Answers (1)

Blackbelt
Blackbelt

Reputation: 157437

try resetting the default color for the other rows if the condition is false:

 if(uas.size() > 0 && tinydb.getInt("selectedUA", 0) == position) {
       item.setBackgroundColor(Color.GREEN);
 } else {
       // e.g.
       item.setBackgroundColor(Color.RED);
 }

Upvotes: 3

Related Questions