1

Desperately trying to get the index of a listview which is fed by an observable collection from a separate viewmodel class.

in my mainpage, which holds the listview xaml(called "mainlist") i have the onitemselected method which needs to give me the index.

I've tried all so far without any luck.

This is the only code that applies but it gives me null reference expception as well:

            int index = (mainlist.ItemsSource as List<MainListItem>).IndexOf(e.SelectedItem as MainListItem);

Saying Object reference not set to an instance of an object.

What am i missing here?

This is my mainviewmodel.cs that creates the observablecollection

private ObservableCollection<MainListItem> _list;

        public ObservableCollection<MainListItem> List
        {
            get { return _list; }
            set
            {
                _list = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(List)));
            }
        }
SunnySonic
  • 1,318
  • 11
  • 37
  • instead of using ItemsSource, try using a reference to the actual list that was assigned to ItemsSource – Jason May 22 '18 at 21:54
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – fredrik May 22 '18 at 21:55
  • Thanks Jason. How would i do that? The list is created in another class. – SunnySonic May 22 '18 at 21:57
  • @fredrik -- completely different. Please check first before marking duplicates! Further more this is in c# – SunnySonic May 22 '18 at 21:58
  • Null pointer is still null pointer - break it down and debug it. – fredrik May 22 '18 at 22:03
  • ok @fredrik. If you have something that would help me would be nice too. I know where the nullpointer is, it's on something that normally functions. It being Xamarin Forms things are different though hence me looking for an alternative to solve this. – SunnySonic May 22 '18 at 22:07
  • sure that it's actually a null pointer? `as` will return null if types do not match. As I said, step thru it with a debugger. – fredrik May 22 '18 at 22:12
  • on the assumption that `mainlist` is a `ListView` control, do you need to do the `.ItemsSource as List` cast? Would `int index = mainlist.IndexOf(e.SelectedItem)` work? – Lindsay May 22 '18 at 23:42
  • Thanks @Lindsay . This doesn't get accepted with the message: FlexExtensions.IndexOf(Item, Item) is inaccessible due to its protection level – SunnySonic May 23 '18 at 00:22
  • Why do you want to do this in your code behind when you even have got a viewModel?! Just bind the selectedItem to a property in your viewModel and use `IndexOf` on your `List` with the selectedItem – Johannes May 23 '18 at 05:43
  • @Johannes that's what doesn't seem to be working. might have made a mistake though. in any case i solved it with the code below. – SunnySonic May 23 '18 at 07:33
  • Could you show the code of what you tried out? I don't think your solution is a proper/clean answer, especially if you already have a ViewModel. – Johannes May 23 '18 at 08:18

1 Answers1

1

Eventually I solved it by looping through the whole list and calculating my own index like so below: I'm aware that there must be a better way, but since my list is relatively small, this serves me well.

int index = 0;
int i = 1;

foreach (MainListItem itemtocheck in mainlist.ItemsSource as ObservableCollection<MainListItem>)
                {
                    if(itemtocheck == e.SelectedItem)
                    {
                        index = i;
                        Debug.WriteLine("Index is " + index);
                        break;
                    }
                    else
                    {

                        Debug.WriteLine("No match for index at " + i);
                        i++;
                    }

                }
SunnySonic
  • 1,318
  • 11
  • 37