1

I made an adapter for my ListView like so :

SimpleAdapter adapter = new SimpleAdapter(this.getBaseContext(), listItem, R.layout.list_cell_icon, new String[] { "img", "title", "description" }, new int[] { R.id.img, R.id.title, R.id.description }) {

        @Override
        public boolean isEnabled (int position) {

            if(position == 1 || position == 2) {
                return false;
            }
            return true;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            View v = super.getView(position, convertView, parent);

            if(position == 1 || position == 2) {

                TextView tv = (TextView) v.findViewById(R.id.title);
                tv.setTextColor(Color.DKGRAY);
            }

            return v;
        }
    };

For now I have two items that I want to disable in the list and then I want to display the text in dark gray. My problem is that the text color in row at position 0 is changed to dark gray as well. How can it be ? Did I miss something?

Alexis
  • 16,629
  • 17
  • 62
  • 107

1 Answers1

1

you need to add this :

TextView tv = (TextView) v.findViewById(R.id.title);    
if(position == 1 || position == 2) {
    tv.setTextColor(Color.DKGRAY);
} else {
    tv.setTextColor(Color.WHITE);
}

The reason is that getView is called several times, not necesarly in a particular order, and getView recycle views. Hence, your TextView object is most likely always the same object. Therefore, you have to set back the color to the view. Or, you can use a state and a selected (like setEnabled(false) on the TextView)

njzk2
  • 38,969
  • 7
  • 69
  • 107
  • If I remove position == 1, only row 0 and 2 are in dark gray. Can this match what you said about recycling? – Alexis Aug 23 '12 at 09:38
  • and then what about tv.setTextColor(getResources().getColor(android.R.color.secondary_text_dark)); for the color ? :) – Alexis Aug 23 '12 at 09:40