Last active
February 25, 2017 20:54
-
-
Save tayyebi/9dcba00240af099f72d9592689ff0827 to your computer and use it in GitHub Desktop.
Custom list adapter Xamarin.Android and direct SQL connection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Android.App; | |
using Android.Widget; | |
using Android.OS; | |
using System.Data.SqlClient; | |
using System.Collections.Generic; | |
namespace MyAndroid | |
{ | |
public class PostOnFirstPage | |
{ | |
public string Id { get; set; } | |
public string Title { get; set; } | |
public string DateTime { get; set; } | |
} | |
[Activity(Label = "MyAndroid", MainLauncher = true, Icon = "@drawable/icon")] | |
public class MainActivity : Activity | |
{ | |
protected override void OnCreate(Bundle bundle) | |
{ | |
base.OnCreate(bundle); | |
// Set our view from the "main" layout resource | |
SetContentView(Resource.Layout.Main); | |
SqlConnection con = new SqlConnection(); | |
con.ConnectionString = About.ConnectionString; | |
con.Open(); | |
SqlCommand com = new SqlCommand(); | |
com.Connection = con; | |
com.CommandText = "SELECT TOP 20 Id, Title, [Date] FROM [Post] ORDER BY [Date] DESC"; | |
SqlDataReader data = com.ExecuteReader(); | |
List<PostOnFirstPage> myPosts = new List<PostOnFirstPage>(); | |
while (data.Read()) | |
{ | |
myPosts.Add(new PostOnFirstPage | |
{ | |
Title = data["Title"].ToString(), | |
DateTime = data["Date"].ToString() | |
}); | |
} | |
if (myPosts.Count == 0) | |
{ | |
Toast.MakeText(this, "هیچ پستی دریافت نشد", ToastLength.Long).Show(); | |
} | |
else | |
{ | |
ListView l1 = FindViewById<ListView>(Resource.Id.listView1); | |
l1.Adapter = new RexaAdapter1(this, myPosts); | |
l1.ItemClick += L1_ItemClick; | |
} | |
} | |
private void L1_ItemClick(object sender, AdapterView.ItemClickEventArgs e) | |
{ | |
// throw new System.NotImplementedException(); | |
} | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using Android.App; | |
using Android.Content; | |
using Android.OS; | |
using Android.Runtime; | |
using Android.Views; | |
using Android.Widget; | |
using Java.Lang; | |
namespace MyAndroid | |
{ | |
class RexaAdapter1 : BaseAdapter | |
{ | |
List<PostOnFirstPage> items; | |
Activity context; | |
public RexaAdapter1(Activity context, IEnumerable<PostOnFirstPage> items) : base() | |
{ | |
this.context = context; | |
this.items = items.ToList(); | |
} | |
public override int Count | |
{ | |
get | |
{ | |
return items.Count(); | |
} | |
} | |
public override Java.Lang.Object GetItem(int position) | |
{ | |
return items[position].Title; | |
} | |
public override long GetItemId(int position) | |
{ | |
return position; | |
} | |
public override View GetView(int position, View convertView, ViewGroup parent) | |
{ | |
View view = convertView; | |
if (view == null) | |
view = context.LayoutInflater.Inflate(Resource.Layout.RexaListItem1, null); | |
view.FindViewById<TextView>(Resource.Id.textView1).Text = items[position].Title; | |
view.FindViewById<TextView>(Resource.Id.textView2).Text = items[position].DateTime; | |
return view; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:orientation="vertical" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:minWidth="25px" | |
android:minHeight="25px"> | |
<TextView | |
android:text="Large Text" | |
android:textAppearance="?android:attr/textAppearanceLarge" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:id="@+id/textView1" /> | |
<TextView | |
android:text="Small Text" | |
android:textAppearance="?android:attr/textAppearanceSmall" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:id="@+id/textView2" /> | |
</LinearLayout> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment