How to display a Dynamic type list in ListView Xamarin Form?

I am developing an application from which I am obtaining data directly from a database, these are stored inside a variable of dynamic type to later insert it in a list and be able to display it in a ListView, but at the moment of executing the application, it doesn´t show me anything on the phone. The reason why I am using a variable of Dynamic type to store the data is because the application must show in a ListView all the tables that are in the Database, even if in the future new tables are inserted into the Database.

That is the reason why I can´t leave already defined models of the structures of the tables, because I can,t know the structures of the tables that will be added in the future to the DB.

I made an example of what I'm trying to do with a Dynamic type list, but it does not show me the data on the screen. I would really appreciate it if you could help me solve it.

public partial class MainPage : ContentPage
{
private ListView lv_inst;
private StackLayout st_inst, stk1;

    public MainPage()
    {
        Title = "List";
        CreateGUIAsync();
    }

    public void CreateGUIAsync()
    {
        lv_inst = new ListView()
        {
            HasUnevenRows = true,
            ItemTemplate = new DataTemplate(typeof(ResultCell10))
        };
        var stk_2 = new StackLayout()
        {
            Orientation = StackOrientation.Vertical,
            HorizontalOptions = LayoutOptions.CenterAndExpand,
            VerticalOptions = LayoutOptions.Center,
            Children =
                {
                    lv_inst
                }
        };

        st_inst = new StackLayout()
        {

            Orientation = StackOrientation.Vertical,
            HorizontalOptions = LayoutOptions.Center,
            VerticalOptions = LayoutOptions.Center,
            Children =
                {
                    stk_2
                }
        };
        Content = st_inst;
    }
    protected override async void OnAppearing()
    {
        base.OnAppearing();
        try
        {
            lv_inst.IsVisible = false;
            List<dynamic> list = new List<dynamic>();
            dynamic dObj = new ExpandoObject();
            dObj.id = "1";
            dObj.name = "Juan";
            dObj.surname = "Moreno";
            dObj.age = "22";
            list.Add(dObj);
            lv_inst.ItemsSource = list;
            lv_inst.IsVisible = true;
        }
        catch (Exception e) { await DisplayAlert("", e.StackTrace, ""); }
    }
}

class ResultCell10 : ViewCell
{
    public ResultCell10()
    {
        int heigh = 35;

        List<String> propierty = new List<string>();
        List<Label> lLabel = new List<Label>();

        propierty.Add("id");
        propierty.Add("name");
        propierty.Add("surname");
        propierty.Add("age");

        var i = 0;

        foreach (var p in propierty)
        {
            lLabel.Add(
                new Label()
                {
                    FontSize = 10,
                    HeightRequest = heigh,
                    WidthRequest = 60,
                    TextColor = Color.Black,
                    FontFamily = "Roboto"
                }
             );
            lLabel[i].SetBinding(Label.TextProperty, p);
            i++;
        }

        var stackList = new StackLayout
        {
            Padding = new Thickness(10),
            Orientation = StackOrientation.Horizontal,
            VerticalOptions = LayoutOptions.Center,
            HorizontalOptions = LayoutOptions.CenterAndExpand,
        };

        foreach (var l in lLabel)
        {
            stackList.Children.Add(l);
        }
        View = stackList;
    }
}

}

Answers

  • marineataemilmarineataemil Member ✭✭

    But can you make an object of the table? I mean if you know the object at least maybe make a bindeble table.

Sign In or Register to comment.