Programmatically set the background color of a list view item

In my app, I have to automatically set the background color of a ListView item, based on the string content of a given list view item.

Currently, the app is using the below code:

    private void UpdateListView(string itemFromList, ListView listView)
    {
        int itemListPosition = listViewAdapter.GetPosition(itemFromList);
        listView.SetItemChecked(itemListPosition, true);

        View child = listView.GetChildAt(itemListPosition);

        if (child != null)
        {
            for (int i = 0; i < listViewAdapter.Count; i++)
            {
                View otherChild = listView.GetChildAt(i);

                if (otherChild != null)
                {
                    otherChild.SetBackgroundColor(defaultColor);
                }
            }

            child.SetBackgroundColor(Color.Green);
        }
    }

where listViewAdapter is a global variable in the MainActivity class defined as below into the onCreate method:

listViewAdapter= new ArrayAdapter<String>(this, Android.Resource.Layout.SimpleListItem1, settingItems);
listView.Adapter = listViewAdapter;

and settingItems is just a global variable defined as a list of strings.

The above code was working fine, so the background color of a given list view item was changing to green and the rest of the list view items to the default color (which is black), until the list view added a vertical scroll to it.

Now after the list view got more items and a vertical scroll all the time the

View child = listView.GetChildAt(itemListPosition);

child object from the above call is null. Consequently, I can't access anymore the items from the list view based on their position.

Please note that the list view item can have the green background if some conditions are meet but then it can also change to the black background if other conditions are meet. So one list item will change its background color depending on some conditions that the app will raise, that is why the app uses the "itemFromList" parameter in "UpdateListView" method.

I am quite new to Xamarin so I hope that it can be an easy thing to achieve this UI behavior.

Do you know how can I work around this ?

Answers

  • jezhjezh Member, Xamarin Team Xamurai

    You can use the following code instead:

      View child = listView.Adapter.GetView(itemListPosition, null, listView);
    
  • MarianaNMarianaN Member

    Thank you for your answers !

    Based on Track320 post, and some more research about how list view is recycling the items, I think that I got a workaround.

    This will be actually the code that I am now testing:

            int itemListPosition = listViewAdapter.GetPosition(itemFromList);
            listView.SmoothScrollToPosition(itemListPosition);
    
            listView.SetItemChecked(itemListPosition, true);
            View child = listView.GetChildAt(itemListPosition);
    

    So actually I expected that the checked item won't be recycled anymore since the "SmoothScrollToPosition" will make it visible on the the phone's display. So the "GetChildAt" wont't return null anymore and I will be able to change the return's view background color.

    Please let me know if I am missing something, or I am wrong with something.

Sign In or Register to comment.