多选Listview [英] Multi-select Listview

查看:80
本文介绍了多选Listview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的列表视图,其中应包含来自我的SQLite数据库的活动列表:

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>

这是我填充列表视图的方式,它将返回至少五(5)个活动:

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);
    }
}

所选项目的绑定:

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

点击获取选定的项目

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?

推荐答案

您的Switch绑定到模型的Selected属性.只需进行迭代(或使用LINQ)即可获取选中的项目.

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;
    ...

这篇关于多选Listview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆