使用功能从sqlite数据库中获取指定数据? [英] Getting specified data out of sqlite database with function?

查看:342
本文介绍了使用功能从sqlite数据库中获取指定数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图获取一个ListView,其中包含数据库中每个项目的名称,但我得到的只是表的全名.而且,如果有可能,我想使用相同的功能稍后再获取其他信息,例如id.如果需要更多信息,请问我.

I am trying to get a ListView with the names of each item inside the database but all I get back is the full table name. And if it is possible I want to use the same function to get other info like id later. If more information is needed ask me.

这是我的代码:

调用函数:

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

先谢谢您

推荐答案

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

Your var categories is a List filled with multiple Categories. The ListView prints the name of the object in every row, which is: Categories.

您应该制作自己的数据模板,以打印每个"类别"的属性名称.

You should make your own datatemplate that prints the attribute name of each "Categories".

像这样声明自己的数据模板:

Declare your own DataTemplate like this:

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

   return new ViewCell { View = nameLabel };
}

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

Next up use your datatemplate to show the name of the categories. Instantiate your ListView like this:

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

然后您的ListView将显示名称.

Then your ListView will show the names.

这篇关于使用功能从sqlite数据库中获取指定数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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