I have a list view that list the activities. I have a way to get the selected values. What I don't know is how can I check if the user doesn't select any items from the list. How can I do this?

This is how I populate my list this will return at least 5-20 activities:

public void Get_Activities()
{
   try
   {
       var db = DependencyService.Get<ISQLiteDB>();
       var conn = db.GetConnection();

       var getActivity = conn.QueryAsync<ActivityTable>("SELECT * FROM tblActivity WHERE Deleted != '1' ORDER BY ActivityDescription");
       var resultCount = getActivity.Result.Count;

       if (resultCount > 0)
       {
           var result = getActivity.Result;
           lstActivity.ItemsSource = result;
           lstActivity.IsVisible = true;
       }
       else
       {
           lstActivity.IsVisible = false;
       }
    }
    catch (Exception ex)
    {
       //Crashes.TrackError(ex);
    }
}

And this is how I get the values of the selected items on my list:

foreach(var x in result)
{
  if (x.Selected) 
  {
    // do something with the selected items
  }
}

My question is like this

if(list.selecteditemcount == 0){
   DisplayAlert("Error","Please select atleast 1 item","Ok");
}

2 Answers

0
Community On Best Solutions

Use this

if (result.Any(x => x.Selected))
{

}
else
{
    await DisplayAlert("Application Error", "Please choose at least one(1) activity", "Ok");
}
0
Elias Johannes On

In xamarin projects you can use Linq (using System.Linq). With Linq it's really easy to filter your list like that:

 if(!list.Any(x => x.Selected == true))
 {
     // DisplayAlert...
 }

This basically checks if any of your items has the value Selected='true'

Or without Linq you can do something like this:

 if(list.FindAll(x => x.Selected).Count() == 0)
 {
     //DisplayAlert...
 }