Filtering events by categories

Eventful.com has a rich set of categories that cover the breadth of events happening around the globe; from music to business events, varied types of events are available. There may be users who are interested only in concert events, while another group of users prefer sports events. Customization that suits the end user's taste is key to an app's success.

The following table shows all categories of events supported by the Eventful API:

Category ID

Category name

music

Concerts & Tour Dates

conference

Conferences & Tradeshows

learning_education

Education

family_fun_kids

Kids & Family

festivals_parades

Festivals

movies_film

Film

food

Food & Wine

fundraisers

Fundraising & charity

art

Art Galleries & Exhibits

support

Health & Wellness

books

Library & Books

attractions

Museums & Attractions

community

Neighborhood

business

Business & Networking

singles_social

Nightlife & Singles

schools_alumni

University & Alumni

clubs_association

Organizations & Meetups

outdoors_recreation

Outdoors & Recreation

performing_arts

Performing Arts

animals

Pets

politics_activisim

Politics & Activism

sales

Sales & Retail

science

Science

religion_spirituality

Religion & Spirituality

sports

Sports

technology

Technology

other

Other and Miscellaneous

Note

Most of the API calls work by using the category ID. The category name is for display purposes; avoid it wherever possible. The category ID is preferred as it is all lowercase, clean, and has no special character-based keyword.

We extend the HelloEvents example we saw previously, and use the Eventful category API to filter events:

  1. Create a new Silverlight for Windows Phone project and Name it HelloEvents-Categories.
  2. Copy the XAML and C# code from the MainPage.xaml and MainPage.xaml.cs files of the HelloEvents example. However, on the home screen, change the text of the Show Events button to List Categories, as we will now be showing a list of event categories and then the actual events, based on the category selection.
  3. Also change the click event method name to showCategories_Click, as shown in the following XAML code for the new List Categories button:
    <Button Content="List Categories" Height="72"
    HorizontalAlignment="Left" Margin="-4,270,0,0" Name="button1"
    VerticalAlignment="Top" Width="231"
    Click="showCategories_Click" />
    
  4. Now on the Click event, we will not be showing the Events Page as we did earlier; instead, we will create a new Windows Phone Portrait Page (CategoryPage.xaml) and use that as the second screen.
    private void showCategories_Click(object sender,
    RoutedEventArgs e)
    {
    NavigationService.Navigate(
    new Uri("/CategoryPage.xaml?latitude=" + latitudeText.Text +
    "&longitude=" + longitudeText.Text, UriKind.Relative)
    );
    }
    
  5. Our Category Page is a simple page that will show the event categories fetched from the Eventful.com API. Add the ProgressBar and ListBox control to your CategoryPage.xaml file to make it look like the following screenshot:
    Filtering events by categories
  6. In our CategoryPage.xaml.cs file, we add the variables similar to our HelloEvents example, however, now we add two more List variables of type string — one to hold the category ID and another to hold the category name.
    public partial class CategoryPage :PhoneApplicationPage
    {
    WebClient myWebClient;
    String apiKey = "xxxxxxxxxxx"; // Get your own key from
    //Eventful
    List<string> myCategoryId;
    List<string> myCategoryName;
    string latitude;
    string longitude;
    
  7. Note, we will not be using the latitude and longitude obtained from the MainPage (the MainPage.xaml.cs file). However, we need these variables to pass to our third page — EventPage.xaml.
  8. In our CategoryPage() constructor, we initialize the following variables:
    myWebClient = new WebClient();
    myCategoryName = new List<string>();
    myCategoryId = new List<string>();
    
  9. Now in our navigation service's OnNavigatedTo method, we store the latitude and longitude obtained from the MainPage (the MainPage.xaml.cs file) and call the Eventful.com API to get a list of all categories.
    Protected override void
    OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
    {
    base.OnNavigatedTo(e);
    latitude = NavigationContext.QueryString["latitude"];
    longitude = NavigationContext.QueryString["longitude"];
    // Load Event Categories from Eventful.com API
    myWebClient.DownloadStringCompleted += new
    DownloadStringCompletedEventHandler
    (myWebClient_DownloadStringCompleted);
    Uri uri = new Uri
    ("http://api.evdb.com/rest/categories/list?app_key="
    + apiKey);
    myWebClient.DownloadStringAsync(uri);
    // End of Eventful API call.
    }
    
  10. We modify the myWebClient_DownloadStringCompleted method and change the name of the XML tags to be parsed, as well as some string replacement to enhance the display.
    private void myWebClient_DownloadStringCompleted(object sender,
    DownloadStringCompletedEventArgs e)
    {
    XElement xml = XElement.Parse(e.Result);
    loadingBar.IsIndeterminate = false;
    loadingBar.Visibility = Visibility.Collapsed; ;
    IEnumerable<XElement> elements = xml.Descendants("category");
    foreach (XElement element in elements)
    {
    String categoryName = element.Element("name").Value.Replace
    ("&amp;", "&");
    String categoryId = element.Element("id").Value;
    myCategoryId.Add(categoryId);
    myCategoryName.Add(categoryName);
    }
    myCategoriesList.ItemsSource = myCategoryName;
    }
    
  11. Notice how we added the category ID and category name to different List variables. We did this purposely, as the index of both of the list items will be same, so mapping the category name to category ID will be easier. As mentioned earlier, the Eventful.com API works well with the category ID and not category name, as the former is cleaner text.
  12. We use the SelectionChanged attribute of the ListBox control to define the method that will be fired when the user selects any item from the ListBox. This is done on the XAML side in our CategoryPage.xaml as:
    <ListBox Height="595" HorizontalAlignment="Left"
    Margin="2,6,0,0" Name="myCategoriesList"
    VerticalAlignment="Top" Width="448"
    SelectionChanged="showEvents">
    
  13. The showEvents method is where the third page of the application is called, and the latitude/longitude pair along with the event category selected is passed as a query string to the third XAML page, which is EventPage.xaml.
    private void showEvents(object sender,
    SelectionChangedEventArgs e)
    {
    String selectedCategoryId =
    myCategoryId[myCategoriesList.SelectedIndex];
    
    NavigationService.Navigate(new
    Uri("/EventPage.xaml?latitude=" + latitude + "&longitude=" +
    longitude + "&categoryId=" + selectedCategoryId,
    UriKind.Relative)
    );
    }
    
  14. Notice the highlighted code in the previous step, where we are getting the category ID corresponding to the category Name selected from the list box. As the List<string> can be accessed by index, we add the appropriate values to the category ID list and category name list (myCategoryId and myCategoryName) and access it as we need. Alternatively we could have chosen to use a multi-dimensional array.
  15. Our EventPage.xaml.cs remains mostly the same as before, except for the addition of the category ID in the Eventful.com API to fetch nearby events. Note, the API URL changes a bit too, as shown in the following lines of code:
    protected override void
    OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
    {
    base.OnNavigatedTo(e);
    string latitude = NavigationContext.QueryString["latitude"];
    string longitude =
    NavigationContext.QueryString["longitude"];
    stringcategoryId =
    NavigationContext.QueryString["categoryId"];
    // Load Events from Eventful.com API
    myWebClient.DownloadStringCompleted +=
    new DownloadStringCompletedEventHandler
    (myWebClient_DownloadStringCompleted);
    Uri uri = new
    Uri("http://api.eventful.com/rest/events/search?category=" +
    categoryId + "&location=" + latitude + "," + longitude +
    "&app_key=" + apiKey + "&within=10");
    myWebClient.DownloadStringAsync(uri);
    // End of Eventful API call.
    }
    
  16. It's emulator time now! Run the application on the emulator; you should get the following screenshot:
    Filtering events by categories
  17. Clicking on the List Categories button loads CategoryPage.xaml as shown in the following screenshot:
    Filtering events by categories
  18. Select any event category; let's say Festivals, which loads EventPage.xaml with events for the selected category, as shown in the following screenshot:
    Filtering events by categories

Observe how the back button works seamlessly; play around with the location and event categories to see different events worldwide.

You can find this example project in the code files for the book underChapter 4, titled HelloEvents-Categories.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.15.137.91