Question

I've an ListView with different count of entries every in runtime (normal -> depends at the user)

I read a post to change the color of the TextViews in the getView() of the Adapter. It worked well, if I wouldn't scroll -> then there are more colored entries...! And I want only ONE.

I'm trying to change only one specific row, and not the others. The others have the 'correct color'.

listview_otherSubnets.setAdapter(new ArrayAdapter<String>(this, R.layout.networks_list_item, list) {
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View view=super.getView(position, convertView, parent);

                TextView textView=(TextView) view.findViewById(R.id.networkslistitem);

                //index_network specifies the specific network row

                /*YOUR CHOICE OF COLOR*/
                if(index_network==-1) {//get the error entry
                //always one entry and if calculation above is not correct then there would be an error text in the TextView
                    textView.setTextColor(Color.parseColor("#ff4444"));
                    textView.setTypeface(null, Typeface.BOLD);
                }

                if(index_network!=-1 && index_network==position){//get the specific one
                    Log.i("OtherSubnet", "Adapter Current Network");
                    textView.setTextColor(Color.parseColor("#33b5e5"));
                    textView.setTypeface(null, Typeface.BOLD);
                }

                //otherwise default color (from the xml) -> see image

                view.setClickable(false);

                return view;
            }
    });

after scrolling (without error message)
after scrolling
correct would be: only one row

Is it possible to get the text of the TextView, if it's on the first run?
The text which should be set?

As I tried, I got an exception.

Was it helpful?

Solution

The adapter class is designed that way, that while scrolling views will be reused, but not created again. Your colored textview, after scrolling is reused, so some other element will be colored. If you scrool again there will be more colored elements. That's why you have to color your marked element with desired color, and color ALL OTHER elements with normal color.

listview_otherSubnets.setAdapter(new ArrayAdapter<String>(this, R.layout.networks_list_item, list) {
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view=super.getView(position, convertView, parent);

            TextView textView=(TextView) view.findViewById(R.id.networkslistitem);

            if(index_network==position){
                Log.i("OtherSubnet", "Adapter Current Network");
                textView.setTextColor(Color.parseColor("#33b5e5"));
                textView.setTypeface(null, Typeface.BOLD);
            } else {
                textView.setTextColor(Color.parseColor("#ff4444"));
                textView.setTypeface(null, Typeface.BOLD);
            }

            view.setClickable(false);
            return view;
        }
});

OTHER TIPS

You have to add one more condition which sets the color to something else.

if(index_network!=-1 && index_network!=position){//get the specific one
    Log.i("OtherSubnet", "Adapter Current Network");
    textView.setTextColor(Color.parseColor("#Something else"));
    textView.setTypeface(null, Typeface.BOLD);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top