Multi-select Listview

2019-08-27 19:47发布

问题:

This is my list view it should contain the list of activities came from my SQLite database:

<ListView SeparatorVisibility="None" x:Name="lstActivity" HasUnevenRows="True">
   <ListView.ItemTemplate>
       <DataTemplate>
           <ViewCell>
               <Frame StyleClass="lstframe" CornerRadius="0" BorderColor="Transparent" HasShadow="False">
                   <StackLayout StyleClass="lstContainer" VerticalOptions="CenterAndExpand">
                       <Grid>
                           <Label StyleClass="lstActivityName" Grid.Row="0" Grid.Column="0" Text="{Binding ActivityDescription}">
                               <Label.FontFamily>
                                    <OnPlatform x:TypeArguments="x:String">
                                        <On Platform="Android" Value="Poppins-Regular.otf#Poppins-Regular"/>
                                     </OnPlatform>
                                </Label.FontFamily>
                           </Label>
                           <Switch Grid.Row="0" Grid.Column="1" IsToggled="{Binding Selected}" />
                       </Grid>
                   </StackLayout>
               </Frame>
            </ViewCell>
       </DataTemplate>
    </ListView.ItemTemplate>
 </ListView>

Here is how I populate the list view this will return atleast five(5) activities:

public void Get_Activities()
{
   try
   {
       var db = DependencyService.Get<ISQLiteDB>();
       var conn = db.GetConnection();

       var getActivity = conn.QueryAsync<ActivityTable>("SELECT * FROM tblActivity WHERE Deleted != '1' ORDER BY ActivityDescription");
       var resultCount = getActivity.Result.Count;

       if (resultCount > 0)
       {
           var result = getActivity.Result;
           lstActivity.ItemsSource = result;
           lstActivity.IsVisible = true;
       }
       else
       {
           lstActivity.IsVisible = false;
       }
    }
    catch (Exception ex)
    {
       //Crashes.TrackError(ex);
    }
}

Selected item binding:

public class SelectData
{
   public bool Selected { get; set; }
}

Get selected Items on click:

private void BtnClose_Clicked(object sender, EventArgs e)
{
   foreach (var x in result)
   {
      if (x.Selected)
      {
         // do something with the selected items
      }
 }
    }

I posted another question regarding on multi-select list view my problem is I don't know how to proceed when I use the answers given to me. How can I get the the selected values because I will save the selected values to my database?

回答1:

your Switch is bound to the Selected property of your model. Just iterate (or use LINQ) to get the items that are selected.

// you need to maintain a reference to result
foreach(var x in result)
{
  if (x.Selected) 
  {
    // do something with the selected items
  }
}

LINQ

var selected = result.Where(x => x.Selected).ToList();

you will also need to have a class level reference to result

// you will need to change this to reflect the actual type of result
List<MyClass> result;

public void Get_Activities()
{
    ...
    result = getActivity.Result;
    ...


回答2:

For multi-select Listview, I have wrote a working example in my blog. Hope this helps : https://androidwithashray.blogspot.com/2018/03/multiselect-list-view-using-xamarin.html?view=flipcard