• Add ListView with Simple List Layout to Show Books in Android App using Xamarin, Visual Studio and C#

    by  • January 3, 2017 • Android with C# and .NET, Xamarin Mobile App Development • 0 Comments

    In this post, we’re going to create a list of books. This is part of our MyBookStore app series and actually we have to build a list of books which looks much better and interesting than what we’re going to create in this post. However, we’ll start with simple list to make sure you understand the process of creating a list. A better list view will be based upon this knowledge and in later posts we’ll build a customized list view with the list of books.

    As a first step, you need to add a ListView control on ViewBooks layout of ViewBooksActivity and rename it to booksListView. To be able to get book details and handle books, we also need to add reference to the MyBookStore.Core library which we created earlier in this series.

    1-add-reference-to-mybookstore-core-libraryNow we can add using statements to refer to the Model and Service namespaces. We can also create the following private variables to hold ListView, all the books, and BookService objects.

    2-add-using-statements-and-create-private-variables

    You also need to make sure that SetContentView method loads the layout for the ViewBooksActivity class. Once loaded, use FindViewById method to load the ListView from the layout and create a new object of BookService class. You also need to load the books into allBooks objects.

    In order to be able to attach the data with the ListView, we need to create a list view Adapter. Just create a folder named Adapters and then add a class in this folder named BookListAdapter.

    3-add-adapters-folder-and-booklistadapter-class

    The code in this class will look like this.

    The GetView method is interesting one in the above class and we’ll explain this method in detail in a later post. For now, you can take it on faith and move on to see simple list in action. Now that our adapter is ready, we need to attach it to the Listview.

    The complete code for the ViewBooksActivity class looks something like this.

    If you run your project now, your app should work just fine and the following list should be displayed if you followed along as I shared in my posts so far.

    4-list-view-with-all-the-books-listed

    I know, this post looks quite simple, but it is important to understand the code and process we used in this post. This is going to be the base of our future posts to create and build more customized lists. If you face any issues while running this code, feel free to leave a comment and I’ll explain and help.

     




    Namecheap.com
    Avatar of admin

    About

    GetCodeSnippet.com provides you high quality Microsoft .NET development related articles, tutorials and sample code for ASP.NET, C#, VB.NET, AJAX, WCF, WPF, Web Services, Windows Services, SQL Server and other technologies.

    Leave a Reply