0

I want to change the background of a TextView, which is inserted in a ListView. The ListView contains a series of TextView, and the value of each one is retrieved from a simple List. Now I show you my snippet:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.town_selection);
    list = (ListView) findViewById(R.id.list);
    adapter = new ArrayAdapter<>(this, R.layout.string_item, App.towns);
    list.setAdapter(adapter);
    list.setOnItemClickListener(this);
    adapter.notifyDataSetChanged();
    list.invalidate();

    // list.getCount() returns zero!!
    // and so, list.getChildAt(...) returns null!!
}

App.towns is the list of String I was talking, and its length is greater than 0. When the app is running it seems ok, because the ListView is showing the items, but at the end of the onCreate I need to change the background color of some items, but the ListView seems not ready as I commented.

notifyDataSetChanged() and invalidate() doesn't help.

Billy Ng
  • 28
  • 4
optimusfrenk
  • 1,271
  • 2
  • 16
  • 34

4 Answers4

1

You will need to write a custom adapter for your list view and change the color when creating each item in the list view.

This SO question - Custom Adapter for List View has great answers which should help you with how to create this custom adapter. You will not be able to do it with the ArrayAdapter as-is.

Community
  • 1
  • 1
Viral Patel
  • 32,418
  • 18
  • 82
  • 110
1

The best solution is to use a custom adapter instead of ArrayAdapter , then in the getView() method of custom adapter you can change the color of the textview

Abakasha Panda
  • 348
  • 3
  • 8
1

If, for some reason, you need to change your background is TextViev, the information about it should be somewhere to store and change the background is using methods in your custom adapter. Create your own custom adapter.

ZigZag
  • 111
  • 2
1

I'm not claiming that my answer is the best, but I have some idea and I'd like to share it.

If your string list of towns, as I understood, is unchangable let's do the following:

...

        final Context context = this;      
        String [] towns = new String [] { "town_1", "town_2", "town_3" };

        ListView list;
        ArrayAdapter<String> adapter;

                @Override
                protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.town_selection);

                list = (ListView) findViewById(R.id.list);
                adapter = new ArrayAdapter<String>(this, R.layout.string_item, R.id.string_item_text, towns)
                //R.id.string_item_text - id your TextView in item of your list
                    { 
                      @Override
                      public View getView(int position, View convertView, ViewGroup parent) {
                         View v = super.getView(position, convertView, parent);
                         TextView txtView = (TextView) v.findViewById(R.id.string_item_text);
                         String sText = txtView.getText().toString();
                         if (sText.equals("town_1")) {
                         txtView.setBackgroundColor(Color.parseColor("#93C47D")); //#93C47D - just example
                         } else if (sText.equals("town_2")){
                         txtView.setBackgroundColor(Color.parseColor("#6FA8DC")); //#6FA8DC - just example
                         } else if (sText.equals("town_3")){
                         txtView.setBackgroundColor(Color.parseColor("#F6B26B"));//#F6B26B - just example
                         }
                     return v;
                     }
                };

            list.setAdapter(adapter);

...

It helped me in one of Android projects. If it will help you it'll be good. In any way, leave your feedback.

EugeneM
  • 36
  • 3