Forum Xamarin.Forms

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

Catch item from ListView and Filter it. Xamarin.Forms

ycusoyycusoy Member ✭✭

This code from https://docs.microsoft.com/en-us/samples/xamarin/xamarin-forms-samples/getstarted-notes-database/
Then on NotesPage.Xaml, I added 2 buttons above the <ListView ..

<ContentPage ..
    <StackLayout>
        <Button Text="DateFirstFromListViewItem" Clicked="OnClickFilterListViewBetwenBtn1TextAndBtn2" x:Name="Button1"/>
        <Button Text="DateLaststFromListViewItem" Clicked="OnClickFilterListViewBetwenBtn2TextAndBtn1" x:Name="Button2"/>
        <ListView x:Name="listView"
              ItemSelected="OnListViewItemSelected">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <TextCell Text="{Binding Text}" 
                          TextColor="Black" 
                          Detail="{Binding Date}" /> <!-- THIS DATE -->
                </DataTemplate>
                ...
    </StackLayout>
</ContentPage>

I have 2 questions, assuming Notes is already filled with data:
1. How do we send Date from the first ListView item to Button1 and the last Date to Button2.
2. When button1 or button2 is clicked, listView will automatically adjust between button1 and button2 date.

Hopefully this can be resolved. Thank you.

Best Answer

  • LeonLuLeonLu Member, Xamarin Team Xamurai
    Accepted Answer

    How do we send Date from the first ListView item to Button1 and the last Date to Button2.

    Open the NotesPage.xaml.cs, find OnAppearing method, change it like following code.

    protected override async void OnAppearing()
            {
                base.OnAppearing();
                List<Note> list =await App.Database.GetNotesAsync();
                listView.ItemsSource = list;
                if (list.Count>0)
                {
                    if (list[0].Text != null)
                    {
                        Button1.Text = list[0].Text;
                    }
    
                    if (list[list.Count - 1].Text != null)
                    {
                        Button2.Text = list[list.Count - 1].Text;
                    }
                }
    
    
    
            }
    

    Here is running screenshot.

    When button1 or button2 is clicked, listView will automatically adjust between button1 and button2 date.

    Could you gave me more information about adjusting between button1 and button2 date automatically?

    Do you want to sort your data in the database? Or other features?


    Xamarin forums are migrating to a new home on Microsoft Q&A!
    We invite you to post new questions in the Xamarin forums’ new home on Microsoft Q&A!
    For more information, please refer to this sticky post.

Answers

  • LeonLuLeonLu Member, Xamarin Team Xamurai
    Accepted Answer

    How do we send Date from the first ListView item to Button1 and the last Date to Button2.

    Open the NotesPage.xaml.cs, find OnAppearing method, change it like following code.

    protected override async void OnAppearing()
            {
                base.OnAppearing();
                List<Note> list =await App.Database.GetNotesAsync();
                listView.ItemsSource = list;
                if (list.Count>0)
                {
                    if (list[0].Text != null)
                    {
                        Button1.Text = list[0].Text;
                    }
    
                    if (list[list.Count - 1].Text != null)
                    {
                        Button2.Text = list[list.Count - 1].Text;
                    }
                }
    
    
    
            }
    

    Here is running screenshot.

    When button1 or button2 is clicked, listView will automatically adjust between button1 and button2 date.

    Could you gave me more information about adjusting between button1 and button2 date automatically?

    Do you want to sort your data in the database? Or other features?


    Xamarin forums are migrating to a new home on Microsoft Q&A!
    We invite you to post new questions in the Xamarin forums’ new home on Microsoft Q&A!
    For more information, please refer to this sticky post.

  • ycusoyycusoy Member ✭✭

    @LeonLu said:

    I got valuable answers from you about the Count function of a database.

    I updated my question, I put the wrong Button there. supposed it is a Datapicker. so like this:

    ....
    <DatePicker x:Name="DatePicker1" PropertyChanged="AutomateDateBetwenDatePicker1AndDatePicker2"/>
            <DatePicker x:Name="DatePicker2" PropertyChanged="AutomateDateBetwenDatePicker2AndDatePicker1"/>
            <ListView x:Name="listView"
                  ItemSelected="OnListViewItemSelected">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <TextCell Text="{Binding Text}" 
                              TextColor="Black" 
                              Detail="{Binding Date}"/> <! - THIS DATE ->
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
    .....
    
    1. How to send the Date of Item listView Detail="{Binding Date}"/><! - THIS DATE -> to DataPicker1 and Last Date to DataPicker2.
    2. When DataPicker1 or DataPicker2 is clicked, the listView automatically adjusts the listView between the DataPicker1 and DataPicker2 dates.
  • ycusoyycusoy Member ✭✭

    @LeonLu
    for the first question I have tried and very satisfying.
    how about the second question?

Sign In or Register to comment.