John Cadac
John Cadac

Reputation: 99

ListView not triggering ItemTapped

I've been working on a school assignment, where you need to load data into a ListView. According to the course manual, you need to use 'ObservableCollection' which I figured out. However, I can't seem to get the ItemTapped to work.

I'm creating the following table using SQLite.

public class Settings
{
    [Primarykey]
    public string Name { get; set; }

    [MaxLength(255)]
    public string Value { get; set; }
}

Then on the OnAppearing I initialise the database and add a row of data.

public partial class SQL : ContentPage
{
    private SQLiteAsyncConnection _connection;
    private ObservableCollection<Settings> _settings;

    public SQL()
    {
        InitializeComponent();


        _connection = DependencyService.Get<ISQLiteDb>().GetConnection();
    }

    protected override async void OnAppearing()
    {
        await _connection.CreateTableAsync<Settings>();

        var settings_name = new Settings { Name = "Meaning of Life", Value = "42" };
        await _connection.InsertAsync(settings_name);

        await DisplayAlert("Alert", "Value Added to database!", "OK");


        var settings = await _connection.Table<Settings>().ToListAsync();
        _settings = new ObservableCollection<Settings>(settings);
        ListView.ItemsSource = _settings;

        base.OnAppearing();
    }

    void MyItemTapped (object sender, System.EventArgs e)
    {
        DisplayAlert("Alert", "You Pressed Something!", "OK");
    }

    void MyItemSelected (object sender, System.EventArgs e)
    {
        DisplayAlert("Alert", "You Selected Something!", "OK");
    }
}

In my XAML file, I have the following, with the ItemTapped going to the function above.

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="BudgetBuddy.SQL">
    <ContentPage.Content>
                <ListView x:Name="ListView">
            <ListView.ItemTemplate ItemTapped="MyItemTapped" ItemSelected="MyItemSelected">
                <DataTemplate>
                    <TextCell Text="{Binding Name}"/>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </ContentPage.Content>
</ContentPage>

I can't figure out what I'm doing wrong. Why doesn't my ItemTapped and ItemSelected work? Also, what would be the best way to access the Value associated with the Name I pressed in the ListView from the ObservableCollection

Upvotes: 2

Views: 1167

Answers (1)

Dr. Banana
Dr. Banana

Reputation: 435

The reason your ItemTapped and ItemSelected aren't working is because they are in the wrong location.

This is what you have:

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="BudgetBuddy.SQL">
    <ContentPage.Content>
                <ListView x:Name="ListView">
            <ListView.ItemTemplate ItemTapped="MyItemTapped" ItemSelected="MyItemSelected">
                <DataTemplate>
                    <TextCell Text="{Binding Name}"/>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </ContentPage.Content>
</ContentPage>

This is what you should do:

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="BudgetBuddy.SQL">
    <ContentPage.Content>
                <ListView x:Name="ListView" ItemTapped="MyItemTapped" ItemSelected="MyItemSelected">
            <ListView.ItemTemplate >
                <DataTemplate>
                    <TextCell Text="{Binding Name}"/>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </ContentPage.Content>
</ContentPage>

Upvotes: 2

Related Questions