Mallow
Mallow

Reputation: 3

How do I bind data from MySQL database to Xamarin ListView?

Working on a project for school with xamarin and online MySQL database, but I'm having trouble. This is my teme.cs, i don't know how to code a reader so it will read all the rows in the database

MySqlConnection con = new MySqlConnection("server=xxx; database=xxx; uid=xxx; pwd=xxxx"); try {

        if (con.State == ConnectionState.Closed)
        {
            con.Open();
            MySqlCommand cmd = new MySqlCommand("SELECT * FROM teme");
            cmd.Connection = con;
            cmd.ExecuteNonQuery();

} }

This is my teme.xaml file where I would like to bind data to the bindings

    <ContentPage.Content>
    <ListView HasUnevenRows="True" x:Name="temeList" SeparatorColor="#B6B6B6">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <Grid Padding="5">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="*" />
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="*" />
                        </Grid.ColumnDefinitions>
                        <Label Grid.Row="0" Text="Naslov:"></Label>
                        <Label Grid.Column="1"
                            Text="{Binding naslov}"
                       FontAttributes="Bold" FontSize="20" />
                        <Label Grid.Row="1" Text="Mentorji:"></Label>
                        <Frame Padding="5" Grid.Column="1" Grid.Row="1" BackgroundColor="#A1CBD9" CornerRadius="8" HorizontalOptions="Start">
                            <Label Grid.Row="1" Grid.Column="1" x:Name="mentorji" VerticalOptions="End" />
                        </Frame>
                        <Label Grid.Row="2" Text="Zasedenost:"></Label>
                        <Frame Padding="5" Grid.Column="1" Grid.Row="2" BackgroundColor="#d9534f" CornerRadius="8" HorizontalOptions="Start">
                            <Label Grid.Row="4" Grid.Column="1" VerticalOptions="End">
                                <Label.FormattedText>
                                    <FormattedString>
                                        <FormattedString.Spans>
                                            <Span Text="{Binding zasedenost}" />
                                            <Span Text="/"></Span>
                                            <Span Text="{Binding max}"></Span>
                                        </FormattedString.Spans>
                                    </FormattedString>
                                </Label.FormattedText>
                            </Label>
                        </Frame>
                        <Label Grid.Row="3" Text="Prostor:"></Label>
                        <Frame Grid.Column="1" Padding="5" Grid.Row="3" CornerRadius="8" BackgroundColor="#5bc0de" HorizontalOptions="Start">
                            <Label Grid.Row="4"
                               Grid.Column="1"
                               Text="{Binding prostor}"
                               VerticalOptions="End" />
                        </Frame>
                    </Grid>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</ContentPage.Content>

Upvotes: 0

Views: 1623

Answers (1)

Jason
Jason

Reputation: 89102

first, don't use ExecuteNonQuery. Second, you will need to create some class to model the data from your db table

List<MyClass> data = new List<MyClass>();

MySqlDataReader rdr = cmd.ExecuteReader();

while (rdr.Read())
{
  var item = new MyClass();
  // here you will need to set the properties of item
  // to the various columns of your DB
  data.Add(item);
}
rdr.Close();

// set the ItemsSource of your ListView
temeList.ItemsSource = data;

Upvotes: 1

Related Questions