Xamarin.Android - List View Using SQL Server Database

Let’s start,

Prior to starting an Android Application, we need to Create SQL server Database Using WEB API. Refer to this one,

Step 1

Open Visual Studio->New Project->Templates->Visual C#->Android->Blank App

Select Blank App. Then give Project Name and Project Location.

Step 2

Go to Solution Explorer-> Project Name-> References then Right Click to Manage Nuget Packages then open new Dialog box. This dialog box to search the Json then Install theNewtonsoft.Json Packages.

Step 3

Next to Open Solution Explorer-> Project Name->Resources->layout->Main.axmlClick Open Design View.

Step 4

Then Go to Toolbar Select ListViewDrag and Drop in Design Page then give android:id="@+id/ListView".

XMLCode 

  1. <?xmlversion="1.0"encoding="utf-8"?>  
  2. <LinearLayout  
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.    android:orientation="vertical"  
  5.    android:layout_width="match_parent"  
  6.    android:layout_height="match_parent"  
  7.    android:minWidth="25px"  
  8.    android:minHeight="25px">  
  9.     <ListView  
  10.       android:minWidth="25px"  
  11.       android:minHeight="25px"  
  12.       android:layout_width="match_parent"  
  13.       android:layout_height="match_parent"  
  14.       android:id="@+id/Listview" />  
  15. </LinearLayout>   

Step 5

Next to create New Layout for List View Design Adapter Page, go to Solution Explorer-> Project Name->Resources->layout then Right Click to Add->New Item then open new Dialog box. Then select Android Layout then give name for ListViewDesign.axml

Step 6

Then open Solution Explorer-> Project Name->Resources->Layout->ListViewDesign.axml click to open Design View then give the  following code, here we create two textviews, one for Name ,another one for Contact Number.

XML Code 

  1. <?xmlversion="1.0"encoding="utf-8"?>  
  2. <LinearLayout  
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.    android:orientation="vertical"  
  5.    android:layout_width="match_parent"  
  6.    android:layout_height="match_parent">  
  7.     <TextView  
  8.       android:text="Name"  
  9.       android:textAppearance="?android:attr/textAppearanceLarge"  
  10.       android:layout_width="match_parent"  
  11.       android:layout_height="wrap_content"  
  12.       android:id="@+id/TxtName" />  
  13.     <TextView  
  14.       android:textAppearance="?android:attr/textAppearanceLarge"  
  15.       android:layout_width="match_parent"  
  16.       android:layout_height="wrap_content"  
  17.       android:id="@+id/TxtNumber"  
  18.       android:text="Number" />  
  19. </LinearLayout>   

Step 7

We need to assign the SQL List Values in any Class so here create new class. Go to Solution Explorer-> Project Name then Right Click to Add->New Item then open new Dialog box. Then select class File give name for UserInfo.cs

Step 8

Then open UserInfo.cs give following code. 

  1. public class UserInfo {  
  2.     publicstring firstname {  
  3.         get;  
  4.         set;  
  5.     }  
  6.     publicstring contact_no {  
  7.         get;  
  8.         set;  
  9.     }  
  10. }   

Step 9

Afterwardsto open Solution Explorer-> Project Name->MainActivity.cs then Add below Namespaces. 

  1. using System.Collections.Generic;  
  2. using Newtonsoft.Json;  
  3. using System.Net.Http.Headers;  
  4. using System.Net.Http;   

Step 10

Next to Open Solution Explorer-> Project Name->MainAtivity.cs,Open Oncreate()then implement following code

C# Code 
  1. public static Context context;  
  2. public static List < UserInfo > UserInfoList = newList < UserInfo > ();  
  3. public static ListView ListView;  
  4. protected override void OnCreate(Bundle bundle) {  
  5.     base.OnCreate(bundle);  
  6.     SetContentView(Resource.Layout.Main);  
  7.     ListView = FindViewById < ListView > (Resource.Id.Listview);  
  8.     GetList list = newGetList();  
  9.     list.Execute();  
  10. }   

Step 11

Below Oncreate() declare the AsyncTask for SQL Server Data Retrieve. Here only we call and assign the values for Base Adapter .



C# Code 
  1. public class GetList: AsyncTask {  
  2.     Context con;  
  3.     protected override Java.Lang.Object DoInBackground(params Java.Lang.Object[] @params) {  
  4.         System.Net.Http.HttpClient client = new System.Net.Http.HttpClient();  
  5.         var _WebApiUrl = string.Format("https://URL/api/Account/Profile");  
  6.         client.DefaultRequestHeaders.Accept.Add(newMediaTypeWithQualityHeaderValue("application/json"));  
  7.         HttpResponseMessage messge = client.GetAsync(_WebApiUrl).Result;  
  8.         var Return_EventList = messge.Content.ReadAsStringAsync().Result;  
  9.         var EventList = JsonConvert.DeserializeObject < List < UserInfo >> (Return_EventList);  
  10.         foreach(var data in EventList) {  
  11.             UserInfoList.Add(data);  
  12.         }  
  13.         return true;  
  14.     }  
  15.     protected override void OnPreExecute() {  
  16.         base.OnPreExecute();  
  17.     }  
  18.     protected override void OnPostExecute(Java.Lang.Object result) {  
  19.         base.OnPostExecute(result);  
  20.         ListView.Adapter = newUserInfoListAdapter(context, UserInfoList);  
  21.     }  
  22. }   

Step 12

Next to create new BaseAdapter class. Declare all variables of ListViewDesign.axml here



C# Code 
  1. class UserInfoListAdapter: BaseAdapter < UserInfo > {  
  2.     private List < UserInfo > mItem = newList < UserInfo > ();  
  3.     private Context context;  
  4.     public UserInfoListAdapter(Context mcontext, List < UserInfo > mItems) {  
  5.         mItem.Clear();  
  6.         mItem = mItems;  
  7.         context = mcontext;  
  8.         this.NotifyDataSetChanged();  
  9.     }  
  10.     public override UserInfothis[int position] {  
  11.         get {  
  12.             return mItem[position];  
  13.         }  
  14.     }  
  15.     public override int Count {  
  16.         get {  
  17.             return mItem.Count;  
  18.         }  
  19.     }  
  20.     public Context MContext {  
  21.         get;  
  22.         privateset;  
  23.     }  
  24.     public override long GetItemId(int position) {  
  25.         return position;  
  26.     }  
  27.     public override View GetView(int position, View convertView, ViewGroup parent) {  
  28.         View listitem = convertView;  
  29.         listitem = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.ListViewDesign, parent, false);  
  30.         TextView TxtName = listitem.FindViewById < TextView > (Resource.Id.TxtName);  
  31.         TextView TxtNumber = listitem.FindViewById < TextView > (Resource.Id.TxtNumber);  
  32.         TxtName.Text = mItem[position].firstname;  
  33.         TxtNumber.Text = mItem[position].contact_no;  
  34.         listitem.Click += (object sender, EventArgs e) => {  
  35.             Toast.MakeText(parent.Context, "Clicked " + mItem[position].firstname, ToastLength.Long).Show();  
  36.         };  
  37.         return listitem;  
  38.     }  
  39. }   

Step 13

Press F5 or Build and Run the Application

Finally, you have successfully created a Xamarin Android List View using SQL Server.


Similar Articles