【Android】4.4 示例--列出手機上的全部聯繫人

分類:C#、Android、VS2015;建立日期:2016-02-06android

項目名:DesignerWalkthroughapp

模板:Blank App(Android)ide

功能:列出手機上的全部聯繫人。工具

說明:該例子提早使用了第9章介紹的列表視圖。佈局

運行效果:

下圖是在模擬器(Galaxy_Api19)下看到的運行效果:this

image

注意:須要先在模擬器的通信錄中添加聯繫人,而後才能看到運行效果。spa

主要設計步驟:

(1)在ListItem.axml中設計列表項模板

新建VS2015項目,模板:「Blank App (Android)」,項目名:DesignerWalkthrough設計

鼠標右擊Resources/layout文件夾,【添加】à【新建項】,在彈出的窗口中,選擇【Android Layout】模板,文件名:ListItem.axml,單擊【添加】按鈕。3d

image

拖放Placeholder.png到drawable文件夾下。調試

從【工具箱】中拖放【ImageView】控件到設計界面中。

從【工具箱】中拖放【LinearLayout (Vertical)】控件到設計界面中,放到【ImageView】的下方。

從【工具箱】中拖放【Text (Large)】控件到設計界面中,放到【LinearLayout (Vertical)】內。

從【工具箱】中拖放【Text (Small)】控件到【Text (Large)】的下方。

image

下面修改佈局,目標是:將ImageView放到兩個Text的左邊:

縮小ImageView的寬度,而後修改根目錄下的LinearLayout控件,在【屬性】窗口中,將其【orientation】屬性改成「horizontal」,即獲得下面的效果:

image

 

技巧:利用【文檔大綱(Document Outline)】選擇要操做的控件,而後再利用【屬性】窗口設置對應的屬性。

設置ImageView的屬性:

src:選擇icon.png圖片,獲得該屬性的值爲「@drawable/icon」。

paddingLeft:0dp

paddingTop:5dp

paddingRight:5dp

paddingBottom:0dp

layoutWidth:50dp

layoutHeight:50dp

adjustViewBounds:true

minWidth:25dp

minHeight:25dp

設置LinearLayout1的屬性:

paddingLeft:0dp

paddingTop:5dp

paddingRight:5dp

paddingBottom:0dp

最終獲得的結果以下:

image

最終獲得的【Source】選項卡中對應的XML以下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <ImageView
        android:src="@drawable/icon"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:id="@+id/imageView1"
        android:adjustViewBounds="true"
        android:paddingLeft="0dp"
        android:paddingRight="5dp"
        android:paddingBottom="0dp"
        android:paddingTop="5dp"
        android:minHeight="25dp"
        android:minWidth="25dp" />
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/linearLayout1"
        android:paddingTop="5dp"
        android:paddingBottom="5dp"
        android:paddingLeft="0dp"
        android:paddingRight="0dp">
        <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>
</LinearLayout>
(2)在Main.axml中添加列表

打開Main.axml。

刪除默認添加的按鈕。

從【工具箱】中拖放一個ListView到設計界面中,而後修改屬性:

id:@+id/listViewContacts

最後獲得的XML以下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:minWidth="25px"
    android:minHeight="25px">
    <ListView
        android:minWidth="25px"
        android:minHeight="25px"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/listViewContacts" />
</LinearLayout>
(3)修改權限配置

修改項目屬性,添加【READ_CONTACTS】權限:

image

 

 

 

修改後,獲得的AndroidMinifest.xml內容以下:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="DesignerWalkthrough.DesignerWalkthrough" android:versionCode="1" android:versionName="1.0" android:installLocation="auto">
    <uses-sdk />
    <application android:label="DesignerWalkthrough" android:icon="@drawable/Icon"></application>
    <uses-permission android:name="android.permission.READ_CONTACTS" />
</manifest>
(4)添加ContactsAdapter.cs

image

 

選擇【Class】模板,輸入文件名,而後將ContactsAdapter.cs的代碼改成下面的內容:

using Android.Views;
using Android.Widget;
using Android.Content;
using Android.App;
using Android.Provider;
using System.Collections.Generic;
namespace DesignerWalkthrough
{
    public class ContactsAdapter : BaseAdapter
    {
        List<Contact> _contactList;
        Activity _activity;

        public ContactsAdapter(Activity activity)
        {
            _activity = activity;

            FillContacts();
        }

        public override int Count
        {
            get { return _contactList.Count; }
        }

        public override Java.Lang.Object GetItem(int position)
        {
            return null;
        }

        public override long GetItemId(int position)
        {
            return _contactList[position].Id;
        }

        public override View GetView(int position, View convertView, ViewGroup parent)
        {
            var view = convertView ?? _activity.LayoutInflater.Inflate(Resource.Layout.ListItem, parent, false);
            var contactName = view.FindViewById<TextView>(Resource.Id.textView1);
            var textView2 = view.FindViewById<TextView>(Resource.Id.textView2);
            var contactImage = view.FindViewById<ImageView>(Resource.Id.imageView1);

            textView2.Text = _contactList[position].Number;

            contactName.Text = _contactList[position].DisplayName;

            if (_contactList[position].PhotoId == null)
            {

                contactImage = view.FindViewById<ImageView>(Resource.Id.imageView1);
                contactImage.SetImageResource(Resource.Drawable.Placeholder);

            }
            else
            {

                var contactUri = ContentUris.WithAppendedId(ContactsContract.Contacts.ContentUri, _contactList[position].Id);
                var contactPhotoUri = Android.Net.Uri.WithAppendedPath(contactUri, ContactsContract.Contacts.Photo.ContentDirectory);

                contactImage.SetImageURI(contactPhotoUri);
            }
            return view;
        }

        void FillContacts()
        {
            var uri = ContactsContract.Contacts.ContentUri;

            string[] projection = {
                ContactsContract.Contacts.InterfaceConsts.Id,
                ContactsContract.Contacts.InterfaceConsts.DisplayName,
                ContactsContract.Contacts.InterfaceConsts.PhotoId
            };

            var cursor = _activity.ContentResolver.Query(uri, projection, null, null, null);

            _contactList = new List<Contact>();

            if (cursor.MoveToFirst())
            {
                do
                {
                    _contactList.Add(new Contact
                    {
                        Id = cursor.GetLong(cursor.GetColumnIndex(projection[0])),
                        DisplayName = cursor.GetString(cursor.GetColumnIndex(projection[1])),
                        PhotoId = cursor.GetString(cursor.GetColumnIndex(projection[2])),
                        Number = "(123) 456 - 7890"
                    });
                } while (cursor.MoveToNext());
            }
        }

        class Contact
        {
            public long Id { get; set; }

            public string DisplayName { get; set; }

            public string PhotoId { get; set; }

            public string Number { get; set; }
        }
    }
}
(5)修改MainActivity.cs

將MainActivity.cs的代碼改成下面的內容:

using Android.App;
using Android.Widget;
using Android.OS;
namespace DesignerWalkthrough
{
    [Activity(Label = "DesignerWalkthrough",
        Theme = "@android:style/Theme.DeviceDefault.Light",
        MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            SetContentView(Resource.Layout.Main);

            var contactsAdapter = new ContactsAdapter(this);
            var contactsListView = FindViewById<ListView>(Resource.Id.listViewContacts);
            contactsListView.Adapter = contactsAdapter;
        }
    }
}
(6)運行

選擇一種模擬器,而後按<F5>鍵調試運行。

相關文章
相關標籤/搜索