【问题标题】:Adding and display array item in List View in Xamarin forms在 Xamarin 表单的列表视图中添加和显示数组项
【发布时间】:2020-10-27 14:05:44
【问题描述】:

[在此处输入图像描述][1]我正在尝试在列表视图中添加和显示选定的数据,但很遗憾无法在列表视图中显示数据,如果有人可以提供帮助,谢谢! (这是我的代码版本)

namespace AppContacts
{
public partial class MainPage : ContentPage
  {
  SQLiteAsyncConnection _connection = DependencyService.Get().GetConnection();

    public MainPage()
    {
        InitializeComponent();
        this.BindingContext = this;
        ReadSelectedData();
    }

    public void ReadSelectedData()
    {
        int[] intArray;
        intArray = new int[4];
        intArray[0] = 175;
        intArray[1] = 197;
        intArray[1] = 757;
        intArray[3] = 915;

        var list = _connection.Table<Contacts>().ToListAsync().Result;
        var myAL = new ArrayList();
        foreach (int rowList in intArray)
        {
            var NewItem = list.Where(x => x.Contact_ID.Equals(rowList));
            myAL.Add(NewItem);
        }
     }
  }

}


新增模型类和XAML页面,XAML页面名称为ContentPage1

XAML 页面 页面名称是 ContentPage1

    <StackLayout>
        <ListView x:Name="listView" HasUnevenRows="True" ItemsSource="{Binding Source={x:Reference ContentPage1}, Path=BindingContext.myAL}" IsVisible="True">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <ViewCell.ContextActions>
                        </ViewCell.ContextActions>
                        <StackLayout Orientation="Vertical" VerticalOptions="CenterAndExpand">
                            <Frame>
                                <StackLayout Orientation="Vertical" VerticalOptions="Center" >
                                    <Label Text="Hello World" HorizontalOptions="Center"></Label>
                                    <Label Text="{Binding Contact_Address}" HorizontalOptions="Center"></Label>
                                    <Label Text="{Binding Contact_eMail}" HorizontalOptions="Center"></Label>
                                </StackLayout>
                            </Frame>
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackLayout>

模型类

using SQLite
namespace AppContacts.Models
{
    public class Contacts
    {
        [PrimaryKey][AutoIncrement]
        public int Contact_ID { get; set; }
        public string Contact_Name { get; set; }
        public string Contact_Address {get; set; }        
        public string Contact_eMail { get; set; }
    }
}

[Output][1]


  [enter image description here][2]


  [1]: https://i.stack.imgur.com/ci5fK.png
  [2]: https://i.stack.imgur.com/ppmp2.jpg

【问题讨论】:

  • 1) 不要在异步操作中使用Result,学习使用 async/await,2) 不要在循环内分配 ItemsSource,在循环完成后执行,3) 你是确定myAL 实际上包含数据?,4) LIstView 的 XAML 在哪里?
  • 您好,您需要先检查list是否包含数据,然后才能检查NewItem是否包含匹配的数据。
  • @Jason 感谢您的及时回复,循环后已经尝试过 ItemSource,也使用了 async/await,myAL 在检查断点时确实包含数据
  • 可能是您的 XAML 有问题,但由于您没有发布,所以很难说
  • 您好@Junior_Jiang 感谢您的回复,列表中包含断点检查时的数据,NewItem 也包含匹配的数据!

标签: arrays listview xamarin


【解决方案1】:

尝试在OnAppearing()中调用ReadSelectedData方法,并为ListView设置ItemsSource

例如:

protected override async void OnAppearing()
{
    base.OnAppearing();

    int[] intArray;
    intArray = new int[4];
    intArray[0] = 175;
    intArray[1] = 197;
    intArray[2] = 757;
    intArray[3] = 915;

    var list = await _connection.Table<Contacts>().ToListAsync().Result;
    var myAL = new ArrayList();
    foreach (int rowList in intArray)
    {
        var NewItem = list.Where(x => x.Contact_ID.Equals(rowList));
        myAL.Add(NewItem);
    }
    listView.ItemSource = myAL;
}

==============================更新================= ===========

我查看了有关Enumerable.Where Method的文档,您可以看到方法返回不是TSource对象。

public static System.Collections.Generic.IEnumerable Where >(this System.Collections.Generic.IEnumerable source, Func >predicate);

它返回一个IEnumerable&lt;TSource&gt; 对象,因此您的代码:

var NewItem = list.Where(x => x.Contact_ID.Equals(rowList));

应该返回一个IEnumerable&lt;Contacts&gt; 对象。

因此,您可以如下修改代码:

foreach (int rowList in intArray)
{
    var NewItem = list.Where(x => x.Contact_ID.Equals(rowList));
    myAL.Add(NewItem.FirstOrDefault());
}

使用NewItem.FirstOrDefault() 获取Contacts 对象。

否则,你不能用Enumerable.Where方法来做,也可以用foreach来实现,如下:

foreach (int rowList in intArray)
{
    foreach (var newitem in list)
    {
        if(newitem.Contact_ID == rowList)
        {
            myAL.Add(newitem);
        }
    }
}

【讨论】:

  • 非常感谢@Junior Jiang-MSFT,确实按照您的建议进行了尝试,在带断点的情况下显示每个数组项,但列表视图仍然是空白的!
  • @EmDe 能不能把myAL的字符串数据显示一下,用断点截图更好?
  • Jian-MSFT,已添加手机截屏,感谢您的帮助!
  • 嗨@Junior Jiang-MSFT,谢谢你一直在帮助我并要求分享链接,请给我一点时间来创建一个新的数据文件与你分享链接,谢谢再次寻求您的帮助!
  • Jiang-MSFT,谢谢,回答很有用我点了标记?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-04
  • 1970-01-01
  • 1970-01-01
  • 2019-02-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多