Multi-select Listview

大兔子大兔子 提交于 2019-12-11 17:19:55

问题


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



来源:https://stackoverflow.com/questions/55184331/multi-select-listview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!