Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

save the state of a switch in a listview

$
0
0

I have a ListView whose itemsource is an observable collection populated from json from an api. I have added a switch to trigger an event that will write the databound (ID, Name, some other int) information to a local sqlite database to minimize data calls back to the database and so I can call it in another page. This all works. What I would like to happen now is that if the user goes back to the page, the switches that they selected will be toggled on.
What I have tried:
I tried to iterate through the list and once it loads, I will try to pull the records from the local db and try to compare them but because I isolate the switch, this has been difficult.
I also looked into the xam.settings plugin to try and save the state of the switches but I haven't seen any examples that use a databound listview and I am still a little fuzzy on mvvm with data.
If anyone has any thoughts on how to go about doing this, I would appreciate it.
Code below:
`

            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <!-- Here we make a Horizontal orientation with the help of StackLayout-->
                        <StackLayout Orientation="Horizontal" Padding="8,0,8,0">
                            <Label x:Name="nmeTxt" x:Uid="{Binding exerciseID}" Text="{Binding exerciseName}" LineBreakMode="CharacterWrap" HorizontalOptions="Start"/>
                            <Label x:Name="xCalCntLbl" Text="{Binding excerciseCalCount,StringFormat='{0:0.0}'}" HorizontalOptions="End" LineBreakMode="WordWrap"/>
                            <Switch Toggled="Switch_Toggled" x:Name="xerSwitch" />
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>`

and in code behind
string Api="myApiLocation";
var content = await _client.GetStringAsync(Api);
var rowContent= JsonConvert.DeserializeObject<List>(content);

                ObservableCollection<exercise> exercises = new ObservableCollection<exercise>(rowContent);

                xerListView.ItemsSource = exercises;

Thank you,
Rob


Viewing all articles
Browse latest Browse all 77050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>