【问题标题】:ListView items not completely loading from SQLite database [Xamarin.Forms]ListView 项目未完全从 SQLite 数据库加载 [Xamarin.Forms]
【发布时间】:2020-02-24 08:05:07
【问题描述】:

我最近注意到我的列表视图和使用 sqlite 和 Xamarin.Forms 的本地数据库存在问题,我的问题是当我的表的数据转储到我的 ObservableCollection(ListView 的源项)并且我的列表视图显示它,ListView 没有显示应该显示的所有项目,我尝试通过将动态(没有数据库)项目添加到我的列表视图中,通过这种方式问题似乎消失了,我确信问题不是来自我的 sqlite 数据库,因为在过去的一个项目中我使用 api 获取我的信息的问题也是一样的,所以我得出的结论是问题是在我的 ListView 中显示外部信息所以有人知道吗一个可能解决我的问题的方法,另一方面,如果有人可以尝试使用 listview 和 sqlite 数据库进行项目,那将非常有帮助。

重要事实:我的 XAML 中的 ListViewFrame 控制器,所以当我不使用它时,它会显示数据库中的所有项目,我也没有不知道为什么

我当前的 XAML 代码:

      <ListView
           SeparatorVisibility="None"
                IsGroupingEnabled="True"
                ItemsSource="{Binding TasksCollection}"
                GroupDisplayBinding="{Binding Key}"
                HasUnevenRows="True">
                <ListView.GroupHeaderTemplate>
                    <DataTemplate>
                        <TextCell
                            Text="{Binding Key}"
                            TextColor="White"/>
                    </DataTemplate>
                </ListView.GroupHeaderTemplate>
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <Frame
                                HeightRequest="150"
                                Margin="10"
                                HasShadow="True"
                                CornerRadius="25"
                                BackgroundColor="White">
                                <Grid
                                    Padding="5">
                                <Grid.GestureRecognizers>
                                    <TapGestureRecognizer Command="{Binding GridExpandCommand}"/>
                                </Grid.GestureRecognizers>
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="2*"/>
                                    <RowDefinition Height="*"/>
                                </Grid.RowDefinitions>
                                    <Label
                                        Grid.Column="0"
                                        Grid.Row="0"
                                        Text="{Binding Name}"
                                        FontAttributes="Bold"
                                        FontSize="Small"
                                        HorizontalOptions="Start"
                                        VerticalOptions="Start">
                                    </Label>
                                    <Image
                                        HeightRequest="25"
                                        Grid.Row="0"
                                        Grid.Column="1"
                                        Source="{Binding TaskIcon}"
                                        HorizontalOptions="End"
                                        VerticalOptions="Start">
                                    </Image>
                                    <Label
                                        Grid.Row="1"
                                        Grid.Column="0"
                                        Text="{Binding Description}"
                                        FontAttributes="Bold"
                                        FontSize="Small"
                                        HorizontalOptions="Start"
                                        VerticalOptions="End">
                                    </Label>
                                    <Button
                                        Grid.Row="1"
                                        Grid.Column="1"
                                        Text="{Binding IsDone}"
                                        TextColor="White"
                                        FontAttributes="Bold"
                                        VerticalOptions="CenterAndExpand"
                                        HorizontalOptions="Center"
                                        FontSize="Small"
                                        CornerRadius="100"
                                        BackgroundColor="LawnGreen">
                                    </Button>
                                </Grid>
                            </Frame>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>

我将项目添加到集合中的代码(我使用 MvvmHelpers 通过 Grouping 对数据进行分组)如下:

foreach (var item in new string[]
            { Languages.HightPriorText, Languages.MediumPriorText, Languages.LessPriorityText })
            {
                var sorted = from tasks in tableList
                             orderby tasks.PriorityLevel // orberby is a keyword of System.Linq namespace
                             group tasks by tasks.PriorityLevel into taskList
                             select new Grouping<string, TaskItemViewModel>(taskList.Key, taskList);
                List<Grouping<string, TaskItemViewModel>> listItems = sorted.Where(t => t.Key == item).ToList();
                foreach (var item2 in listItems)
                    VeryImportantList.Add(item2);
            }

结果如下:

The image of the result of the app

我会注意到任何答案,并且对于任何问题,请提供一些帮助!

【问题讨论】:

  • Quiroz,我无法重现你的问题,所以你能在github上提供一个可以重现这个问题的样本,我会下载你的样本进行测试。
  • “我确定问题不是来自我的 sqlite 数据库” - 你确定,但你没有检查?您能否验证从您的数据库加载的项目是否包含您怀疑它们包含的数据?
  • 能否将您用于加载项目的代码添加到您的TaskCollection
  • @PaulKertscher 好吧,我已经检查过了,不,问题似乎不在我的 sql 数据库中,而似乎是显示项目,我检查了它和我的集合中的数据已正确转储,但问题出在它的显示上
  • @PaulKertscher 是的!我已经编辑了我的帖子并包含了添加项目的代码

标签: c# android sqlite xamarin.forms xamarin.forms.listview


【解决方案1】:

我终于解决了它,正如我所料,这是我当前版本的 Xamarin.Forms (4.0.0) 的一个错误,所以我更新到它的最新版本,一切都恢复正常,正如我所料!

【讨论】:

  • 也为我工作。在我的情况下,问题是使用 OnAppearing 方法添加新项目导致列表有空单元格。更新表单解决了问题
猜你喜欢
  • 1970-01-01
  • 2020-07-19
  • 2019-07-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多