【Android】4.4 示例--列出手机上的所有联系人
分类:C#、Android、VS2015;创建日期:2016-02-06
项目名:DesignerWalkthrough
模板:Blank App(Android)
功能:列出手机上的所有联系人。
说明:该例子提前使用了第9章介绍的列表视图。
运行效果:
下图是在模拟器(Galaxy_Api19)下看到的运行效果:

注意:需要先在模拟器的通讯录中添加联系人,然后才能看到运行效果。
主要设计步骤:
(1)在ListItem.axml中设计列表项模板
新建VS2015项目,模板:“Blank App (Android)”,项目名:DesignerWalkthrough
鼠标右击Resources/layout文件夹,【添加】à【新建项】,在弹出的窗口中,选择【Android Layout】模板,文件名:ListItem.axml,单击【添加】按钮。

拖放Placeholder.png到drawable文件夹下。
从【工具箱】中拖放【ImageView】控件到设计界面中。
从【工具箱】中拖放【LinearLayout (Vertical)】控件到设计界面中,放到【ImageView】的下方。
从【工具箱】中拖放【Text (Large)】控件到设计界面中,放到【LinearLayout (Vertical)】内。
从【工具箱】中拖放【Text (Small)】控件到【Text (Large)】的下方。

下面修改布局,目标是:将ImageView放到两个Text的左边:
缩小ImageView的宽度,然后修改根目录下的LinearLayout控件,在【属性】窗口中,将其【orientation】属性改为“horizontal”,即得到下面的效果:

技巧:利用【文档大纲(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
最终得到的结果如下:

最终得到的【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】权限:

修改后,得到的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

选择【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[])),
DisplayName = cursor.GetString(cursor.GetColumnIndex(projection[])),
PhotoId = cursor.GetString(cursor.GetColumnIndex(projection[])),
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>键调试运行。
【Android】4.4 示例--列出手机上的所有联系人的更多相关文章
- Android popupwindow在低版本手机上无法显示
popupwindow偶尔的显示失效(在低版本Android系统的手机上,测试机6.0)实在是坑害了不少人,害,而且坑了for a long time.本小白就是其中一个受害者. 百度了N久N多还是没 ...
- android的开发 华为手机上不显示menu键
android的开发,华为手机上不显示menu键解决办法: 在AndroidManifest.xml中讲targetSdkVersion改为9. <uses-sdk android:minSdk ...
- HTML5定稿了,终于有一种编程语言开发的程序可以在Android和IOS两种设备上运行了
2007 年 W3C (万维网联盟)立项 HTML5,直至 2014 年 10 月底,这个长达八年的规范终于正式封稿. 过去这些年,HTML5 颠覆了 PC 互联网的格局,优化了移动互联网的体验,接下 ...
- 如何在 Android 手机上实现抓包?
如何在 Android 手机上实现抓包? http://www.zhihu.com/question/20467503 我想知道某个应用究竟在数据提交到哪里,提交了什么.网上的教程太复杂,不想麻烦.有 ...
- 利用exif.js解决ios或Android手机上传竖拍照片旋转90度问题
html5+canvas进行移动端手机照片上传时,发现ios手机上传竖拍照片会逆时针旋转90度,横拍照片无此问题:Android手机没这个问题. 因此解决这个问题的思路是:获取到照片拍摄的方向角,对非 ...
- 如何在Android手机上进行自动化测试(下)
版权声明:允许转载,但转载必须保留原链接:请勿用作商业或者非法用途 前言 通过阅读本篇教程,你将会了解到: 如何使用Poco对Android原生应用进行测试 Poco支持直接对任何Android原生应 ...
- Mac电脑如何读取Android手机上的文件
问题 一般Android手机用usb数据线连接到windows操作系统的电脑上后,会自动将手机存储卡以移动存储的方式显示在电脑里. 但是如果操作系统是Mac的,就没有这个存储设备.问题来了,Mac电脑 ...
- 如何通过wifi在android手机上安装调试应用
如何通过wifi在android手机上安装调试应用 1. 首先还是要打开手机的usb调试选项,并通过usb线连接手机.2. 然后执行“adb tcpip 5555”,把adb从usb模式切换到tcpi ...
- delphi xe5 android 手机上使用sqlite
本篇我们介绍一下在android手机上怎样使用sqlite数据库,这里用Navigator实现 增删改查. 1.新建firemonkey mobile application 2.选择blank ap ...
随机推荐
- poj 2391 (Floyd+最大流+二分)
题意:有n块草地,一些奶牛在草地上吃草,草地间有m条路,一些草地上有避雨点,每个避雨点能容纳的奶牛是有限的,给出通过每条路的时间,问最少需要多少时间能让所有奶牛进入一个避雨点. 两个避雨点间可以相互到 ...
- 第一节,学习cocos2d-x的前期准备
1,我用的mac系统,在mac系统上装上cocos2d-x的模板 2,用doxygen工具装上API,这个非常重要,没有API的开发不叫开发,因此我们要习惯看API 3,知道怎么查看cocos2d-x ...
- 谷歌地图api訪问失败
在非外网情况下.我们调用谷歌api会出现载入不到地图的现象.此时能够换一下域名试试或许就好了 比方我自己訪问api时时这样写的: https://maps.googleapis.com/maps/ap ...
- cookie 设置有效期 检测cookie
设置cookie 函数直接上代码: function setCookie(name, value, days) { //设置cookie var d = new Date(); d.setTime(d ...
- H5中JavaScript常用代码片段
/** * 批量替换方法,批量过滤特殊字符,通常用在通过后的各种编辑器添加的内容在App上编辑上使用 * james.wang 2016-11-11 * 使用方法:ReCont(Content,[&q ...
- 恭喜您成为2014年度Microsoft MVP!
- Chrome实用调试技巧
如今Chrome浏览器无疑是最受前端青睐的工具,原因除了界面简洁.大量的应用插件,良好的代码规范支持.强大的V8解释器之外,还因为Chrome开发者工具提供了大量的便捷功能,方便我们前端调试代码,我们 ...
- 原生php如何获取当前页面的url
原生php如何获取当前页面的url? //php获取当前访问的完整url地址 function get_current_url(){ $current_url='http://'; if(isset( ...
- IDEA删除项目
IDEA没有eclipse的右键直接在磁盘delete整个项目的功能,使用IDEA删除项目需要按照如下步骤: step1:右击项目——>Remove Module 之后会出现提示框如下: 意思是 ...
- java多线程(二)之实现Runnable接口
一.java多线程方式2: 实现Runnable接口 好处:a. 可以避免由于java单继承带来的局限性. b. 适合多个相同的程序的代码去处理同一个资源的情况, 把线程与程序的代码, 数据有效分离, ...