0

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.

This is my code:

calling the function:

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

function:

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

table:

public class Categories
{
    public Categories()
    {
    }

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

thank you in advance,

1
  • GetCategoryByMenuID's return trype is List<Categories> and how can it return full table name ? Commented Nov 23, 2016 at 13:04

1 Answer 1

1

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

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
}

Then your ListView will show the names.

Sign up to request clarification or add additional context in comments.

1 Comment

after using you're code I get this error back: System.ArgumentNullException: Value cannot be null. Parameter name: item. .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.