forked from hussien89aa/XamarinAndroidTutorial
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ReadContact_withphone.cs
84 lines (75 loc) · 2.6 KB
/
ReadContact_withphone.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Provider;
using Android.Database;
using System.Collections.Generic;
namespace ReadContact
{
[Activity(Label = "ReadContact", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
TextView textView1;
Button button;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
// Get our button from the layout resource,
// and attach an event to it
button = FindViewById<Button>(Resource.Id.MyButton);
textView1 = FindViewById<TextView>(Resource.Id.textView1);
button.Click += delegate {
FillContacts();
};
}
void FillContacts()
{
var uri = ContactsContract.CommonDataKinds.Phone.ContentUri;
string[] projection = {
ContactsContract.CommonDataKinds.Phone.Number,
ContactsContract.Contacts.InterfaceConsts.DisplayName,
ContactsContract.Contacts.InterfaceConsts.PhotoId
};
//select id,DisplayName,PhotoId where id=1
var loader = new CursorLoader(this, uri, projection, null, null, null);
var cursor = (ICursor)loader.LoadInBackground();
List<Contact> contactList = new List<Contact>();
if (cursor.MoveToFirst())
{
do
{
contactList.Add(
new Contact(cursor.GetString(cursor.GetColumnIndex(projection[0])),
cursor.GetString(cursor.GetColumnIndex(projection[1])),
cursor.GetString(cursor.GetColumnIndex(projection[2]))
)
);
} while (cursor.MoveToNext());
}
string items = "";
foreach ( Contact c in contactList)
{
items += c.DisplayName + "," + c.Number + "," + c.PhotoId;
}
textView1.Text = items;
}
class Contact
{
public string Number;
public string DisplayName;
public string PhotoId;
public Contact(string Number, string DisplayName, string PhotoId)
{
this.Number = Number;
this.DisplayName = DisplayName;
this.PhotoId = PhotoId;
}
}
}
}