0

I have following code inside GetView method of my Custom adapter:

public override View GetView(int position, View convertView, ViewGroup parent)
    {
        var view = convertView ?? activity.LayoutInflater.Inflate(
                       Resource.Layout.ScannedTuListItem, parent, false);

        var scannedTu = view.FindViewById<TextView>(Resource.Id.scannedTu);

        var tuTxt = activity.Resources.GetString(Resource.String.tu);
        var custTxt = activity.Resources.GetString(Resource.String.customer);
        var orderTxt = activity.Resources.GetString(Resource.String.order);
        var targetBinTxt = activity.Resources.GetString(Resource.String.targetBin);

        scannedTu.Text = 
                        $"{tuTxt} {tus[position].No}" +
                        $"\n{custTxt} {tus[position].Customer} / {orderTxt} {tus[position].Order}" +
                        (string.IsNullOrEmpty(tus[position].TargetBin) ?
                        string.Empty : $"\n{targetBinTxt} {tus[position].TargetBin}");

        if (tus[position].AtPackingStation && !tus[position].Ready)
        {
            scannedTu.SetBackgroundColor(Color.Yellow);
            scannedTu.SetTextColor(Color.Black);
        }
        else if (tus[position].AtPackingStation && tus[position].Ready || tus[position].ScanOk == true)
        {
            scannedTu.SetBackgroundColor(Color.Green);
            scannedTu.SetTextColor(Color.Black);
        }
        else if (tus[position].ScanOk == false)
        {
            scannedTu.SetBackgroundColor(Color.Red);
            scannedTu.SetTextColor(Color.Black);
        }

        return view;
    }

When I change Property ScanOK and Ready to True, say, for the first 3 items in the list, it automatically sets the background color of items outside the screen also to Green.

How do I make it color only the first 3 items without affecting items off the screen?

bigb055
  • 198
  • 3
  • 14
  • Could you offer more code about your items of listview ? what ‘s mean about `tus[position].Ready ` `tus[position].ScanOk` `tus[position].AtPackingStation`? You could debug it, check `if/else` whether meet the requirement. – Leon Feb 05 '19 at 02:34
  • @Leon Lu - MSFT I've debugged it already and e.g. for 10 items if only first 3 meet the critaria and are actually visible on screen it still changes background color of some of the other items. It seems to be doing it randomly... – bigb055 Feb 05 '19 at 10:20

1 Answers1

0

When I change Property ScanOK and Ready to True, say, for the first 3 items in the list, it automatically sets the background color of items outside the screen also to Green.

How do I make it color only the first 3 items without affecting items off the screen?

For that you will have to add an initial condition on the basis of position for eg

   If(position>2) //since integers start from zero
   return;

Also if you want to mark on the basis of how many list items are displayed on the screen you can check this SO answer

Community
  • 1
  • 1
FreakyAli
  • 13,349
  • 3
  • 23
  • 63
  • what if number of items to change color is larger than number of items visible on screen? I want all the items that meet certaing condition to change color even when off screen. How do I know the position of last visible item inside `GetView()`? – bigb055 Feb 05 '19 at 10:17
  • I have attached a Stack answer that handles that did you check it? – FreakyAli Feb 05 '19 at 10:53