【问题标题】:Getting specified data out of sqlite database with function?使用函数从sqlite数据库中获取指定数据?
【发布时间】:2017-04-07 10:22:53
【问题描述】:

我正在尝试获取带有数据库中每个项目名称的ListView,但我得到的只是完整的表名。如果可能的话,我想稍后使用相同的功能来获取其他信息,例如 id 。如果需要更多信息,请询问我。

这是我的代码:

调用函数:

var categories = App.Database.GetCategoryByMenuID(1);
listView.ItemsSource = categories;

功能:

public List<Categories> GetCategoryByMenuID(int menuID)
{
    return db.Table<Categories>().Where(x => x.Menu_ID == menuID).ToList();
}

表:

public class Categories
{
    public Categories()
    {
    }

    [PrimaryKey, AutoIncrement]
    public int ID { get; set; }
    public int Menu_ID { get; set; }
    public string Name { get; set; }
}

提前谢谢你,

【问题讨论】:

  • GetCategoryByMenuID的返回trype是List&lt;Categories&gt;,它怎么能返回full table name

标签: c# android sqlite xamarin xamarin.forms


【解决方案1】:

您的 var 类别 是一个包含多个 类别 的列表。 ListView 在每一行打印对象的名称,即:Categories。

您应该制作自己的数据模板,打印每个“类别”的属性名称。

像这样声明你自己的 DataTemplate:

var datatemplate = new DataTemplate(() =>
{
   var nameLabel = new Label();
   nameLabel.SetBinding(Label.TextProperty, "Name");

   return new ViewCell { View = nameLabel };
}

接下来使用您的数据模板来显示类别的名称。像这样实例化您的 ListView:

var listView = new ListView
{
     ItemsSource = categories,
     ItemTemplate = datatemplate
}

然后您的 ListView 将显示名称。

【讨论】:

  • 使用您的代码后,我收到此错误:System.ArgumentNullException: Value cannot be null。参数名称:项目。 .
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多