ContactManager示例解析
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.contactmanager"
android:versionCode="1" android:versionName="1.0">
<application android:label="@string/app_name" android:icon="@drawable/icon">
<activity android:name=".ContactManager" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="ContactAdder" android:label="@string/addContactTitle">
</activity>
</application>
<uses-sdk android:minSdkVersion="5" android:targetSdkVersion="5" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
</manifest>
res/drawable-xxx
drawable-hdpi/icon.png 72x72
drawable-mdpi/icon.png 48x48
drawable-ldpi/icon.png 32x32
contact_manager.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView android:layout_width="fill_parent"
android:id="@+id/contactList"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<CheckBox android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/showInvisible"
android:text="@string/showInvisible"/>
<Button android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/addContactButton"
android:text="@string/addContactButtonLabel"/>
</LinearLayout>
在ContactManager.onCreate方法中
复选框没有选中
CheckBox mShowInvisibleControl = (CheckBox) findViewById(R.id.showInvisible);
boolean mShowInvisible = false;
mShowInvisibleControl.setChecked(mShowInvisible);
复选框事件
mShowInvisibleControl.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Log.d(TAG, "mShowInvisibleControl changed: " + isChecked);
mShowInvisible = isChecked;
populateContactList();
}
});
"addAccount"按钮及其事件
Button mAddAccountButton = (Button) findViewById(R.id.addContactButton);
mAddAccountButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.d(TAG, "mAddAccountButton clicked");
launchContactAdder();
}
});
在populateContactList()方法中
获取Cursor并构造SimpleCursorAdapter对象
Cursor cursor = getContacts();
String[] fields = new String[] {
ContactsContract.Data.DISPLAY_NAME
};
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.contact_entry, cursor,
fields, new int[] {R.id.contactEntryText});
获取ListView并设置Adapter对象
ListView mContactList = (ListView) findViewById(R.id.contactList);
mContactList.setAdapter(adapter);
在getContacts()方法中
获取Uri、projection、selection, selectionArgs, sortOrder
Uri uri = ContactsContract.Contacts.CONTENT_URI;
String[] projection = new String[] {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME
};
String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '" +
(mShowInvisible ? "0" : "1") + "'";
String[] selectionArgs = null;
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
调用managedQuery方法
return managedQuery(uri, projection, selection, selectionArgs, sortOrder);
在launchContactAdder()方法中
跳转到ContactAdder界面
Intent i = new Intent(this, ContactAdder.class);
startActivity(i);
layout/contact_adder.xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableLayout android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableRow>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/targetAccountLabel"/>
</TableRow>
<TableRow>
<Spinner android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_weight="1"
android:id="@+id/accountSpinner"/>
</TableRow>
<TableRow>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/contactNameLabel"/>
</TableRow>
<TableRow>
<EditText android:id="@+id/contactNameEditText"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="1"/>
</TableRow>
<TableRow>
<TextView android:text="@string/contactPhoneLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</TableRow>
<TableRow>
<EditText android:id="@+id/contactPhoneEditText"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="1"/>
<Spinner android:id="@+id/contactPhoneTypeSpinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</TableRow>
<TableRow>
<TextView android:text="@string/contactEmailLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</TableRow>
<TableRow>
<EditText android:id="@+id/contactEmailEditText"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="1"/>
<Spinner android:id="@+id/contactEmailTypeSpinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</TableRow>
<TableRow>
<Button android:layout_height="wrap_content"
android:text="@string/save"
android:id="@+id/contactSaveButton"
android:layout_width="fill_parent"
android:layout_weight="1"/>
</TableRow>
</TableLayout>
</ScrollView>
在AccountData构造方法(如下)中
AccountData(String name, AuthenticatorDescription description)
mType = description.type;
String packageName = description.packageName;
PackageManager pm = getPackageManager();
mTypeLabel = pm.getText(packageName, description.labelId, null);
10-31 15:20:21.576: INFO/contactMgr(14321): description.type = com.sonyericsson.localcontacts
10-31 15:20:21.576: INFO/contactMgr(14321): description.packageName = com.sonyericsson.localcontacts
10-31 15:20:21.576: INFO/contactMgr(14321): description.labelId = 2130968576
10-31 15:20:21.576: INFO/contactMgr(14321): mTypeLabel = 手机联系人
10-31 15:20:21.576: INFO/contactMgr(14321): description.type = com.google
10-31 15:20:21.576: INFO/contactMgr(14321): description.packageName = com.google.android.gsf
10-31 15:20:21.576: INFO/contactMgr(14321): description.labelId = 2131165435
10-31 15:20:21.576: INFO/contactMgr(14321): mTypeLabel = Google
layout/account_entry.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip">
<ImageView
android:id="@+id/accountIcon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_marginRight="6dip" />
<TextView
android:id="@+id/secondAccountLine"
android:layout_width="fill_parent"
android:layout_height="26dip"
android:layout_toRightOf="@id/accountIcon"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:singleLine="true"
android:ellipsize="marquee"
android:textColor="@android:color/secondary_text_light" />
<TextView
android:id="@+id/firstAccountLine"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/accountIcon"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_above="@id/secondAccountLine"
android:layout_alignWithParentIfMissing="true"
android:gravity="center_vertical"
android:textColor="@android:color/primary_text_light"/>
</RelativeLayout>
在AccountAdapter.getDropDownView(int position, View convertView, ViewGroup parent)方法中
1. 构造convertView
if (convertView == null) {
LayoutInflater layoutInflater = getLayoutInflater();
convertView = layoutInflater.inflate(R.layout.account_entry, parent, false);
}
2. 获取convertView中的子View
TextView firstAccountLine = (TextView) convertView.findViewById(R.id.firstAccountLine);
TextView secondAccountLine = (TextView) convertView.findViewById(R.id.secondAccountLine);
ImageView accountIcon = (ImageView) convertView.findViewById(R.id.accountIcon);
3. 给子View赋值
AccountData data = getItem(position);
firstAccountLine.setText(data.getName());
secondAccountLine.setText(data.getTypeLabel());
Drawable icon = data.getIcon();
if (icon == null) {
icon = getResources().getDrawable(android.R.drawable.ic_menu_search);
}
accountIcon.setImageDrawable(icon);
在AccountAdapter构造方法中
1. 设置DropDownViewResource
setDropDownViewResource(R.layout.account_entry);
在接口OnAccountsUpdateListener中
1. 声明抽象方法onAccountsUpdated
public abstract void onAccountsUpdated(Account[] paramArrayOfAccount);