Add sample for XML-defined Adapters.
Change-Id: I31f4f93eeea0f0cac4485ce76d9188608567f071
This commit is contained in:
16
samples/XmlAdapters/Android.mk
Normal file
16
samples/XmlAdapters/Android.mk
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
LOCAL_PATH:= $(call my-dir)
|
||||||
|
include $(CLEAR_VARS)
|
||||||
|
|
||||||
|
LOCAL_MODULE_TAGS := samples
|
||||||
|
|
||||||
|
# Only compile source java files in this apk.
|
||||||
|
LOCAL_SRC_FILES := $(call all-java-files-under, src)
|
||||||
|
|
||||||
|
LOCAL_PACKAGE_NAME := XmlAdaptersSample
|
||||||
|
|
||||||
|
LOCAL_PROGUARD_ENABLED := disabled
|
||||||
|
|
||||||
|
include $(BUILD_PACKAGE)
|
||||||
|
|
||||||
|
# Use the following include to make our test apk.
|
||||||
|
include $(call all-makefiles-under,$(LOCAL_PATH))
|
||||||
31
samples/XmlAdapters/AndroidManifest.xml
Normal file
31
samples/XmlAdapters/AndroidManifest.xml
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<!-- Copyright (C) 2008 The Android Open Source Project
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
-->
|
||||||
|
|
||||||
|
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
package="com.example.android.xmladapters">
|
||||||
|
|
||||||
|
<uses-permission android:name="android.permission.READ_CONTACTS" />
|
||||||
|
|
||||||
|
<application android:label="@string/app_name">
|
||||||
|
<activity android:name="ContactsListActivity">
|
||||||
|
<intent-filter>
|
||||||
|
<action android:name="android.intent.action.MAIN" />
|
||||||
|
<category android:name="android.intent.category.LAUNCHER" />
|
||||||
|
</intent-filter>
|
||||||
|
</activity>
|
||||||
|
</application>
|
||||||
|
|
||||||
|
</manifest>
|
||||||
BIN
samples/XmlAdapters/res/drawable-hdpi/ic_contact_picture.png
Normal file
BIN
samples/XmlAdapters/res/drawable-hdpi/ic_contact_picture.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.4 KiB |
39
samples/XmlAdapters/res/layout/contact_item.xml
Normal file
39
samples/XmlAdapters/res/layout/contact_item.xml
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<!-- Copyright (C) 2010 The Android Open Source Project
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
-->
|
||||||
|
|
||||||
|
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:gravity="center_vertical"
|
||||||
|
android:minHeight="?android:attr/listPreferredItemHeight">
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/name"
|
||||||
|
android:layout_width="0px"
|
||||||
|
android:layout_weight="1.0"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:textAppearance="?android:attr/textAppearanceLarge"
|
||||||
|
android:gravity="center_vertical"
|
||||||
|
android:drawablePadding="6dip"
|
||||||
|
android:paddingLeft="6dip"
|
||||||
|
android:paddingRight="6dip" />
|
||||||
|
|
||||||
|
<ImageView
|
||||||
|
android:id="@+id/star"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content" />
|
||||||
|
|
||||||
|
</LinearLayout>
|
||||||
33
samples/XmlAdapters/res/layout/contacts_list.xml
Normal file
33
samples/XmlAdapters/res/layout/contacts_list.xml
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<!-- Copyright (C) 2010 The Android Open Source Project
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
-->
|
||||||
|
|
||||||
|
|
||||||
|
<merge xmlns:android="http://schemas.android.com/apk/res/android">
|
||||||
|
|
||||||
|
<ListView
|
||||||
|
android:id="@android:id/list"
|
||||||
|
android:adapter="@xml/contacts"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent" />
|
||||||
|
|
||||||
|
<TextView android:id="@android:id/empty"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
android:gravity="center"
|
||||||
|
android:text="@string/no_contacts"
|
||||||
|
android:visibility="gone" />
|
||||||
|
|
||||||
|
</merge>
|
||||||
20
samples/XmlAdapters/res/values/strings.xml
Normal file
20
samples/XmlAdapters/res/values/strings.xml
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<!-- Copyright (C) 2010 The Android Open Source Project
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASI
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
-->
|
||||||
|
|
||||||
|
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
|
||||||
|
<string name="app_name">Xml Contacts Adapter</string>
|
||||||
|
<string name="no_contacts">No contacts available</string>
|
||||||
|
</resources>
|
||||||
30
samples/XmlAdapters/res/xml/contacts.xml
Normal file
30
samples/XmlAdapters/res/xml/contacts.xml
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<!-- Copyright (C) 2010 The Android Open Source Project
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
-->
|
||||||
|
|
||||||
|
<cursor-adapter xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:uri="content://com.android.contacts/contacts"
|
||||||
|
android:selection="has_phone_number=1"
|
||||||
|
android:layout="@layout/contact_item">
|
||||||
|
|
||||||
|
<bind android:from="display_name" android:to="@id/name" android:as="string" />
|
||||||
|
<bind android:from="starred" android:to="@id/star" android:as="drawable">
|
||||||
|
<map android:fromValue="0" android:toValue="@android:drawable/star_big_off" />
|
||||||
|
<map android:fromValue="1" android:toValue="@android:drawable/star_big_on" />
|
||||||
|
</bind>
|
||||||
|
<bind android:from="_id" android:to="@id/name"
|
||||||
|
android:as="com.example.android.xmladapters.ContactPhotoBinder" />
|
||||||
|
|
||||||
|
</cursor-adapter>
|
||||||
@@ -0,0 +1,96 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2010 The Android Open Source Project
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.example.android.xmladapters;
|
||||||
|
|
||||||
|
import android.content.ContentUris;
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.res.Resources;
|
||||||
|
import android.database.Cursor;
|
||||||
|
import android.graphics.Bitmap;
|
||||||
|
import android.graphics.BitmapFactory;
|
||||||
|
import android.graphics.drawable.BitmapDrawable;
|
||||||
|
import android.graphics.drawable.Drawable;
|
||||||
|
import android.net.Uri;
|
||||||
|
import android.provider.ContactsContract;
|
||||||
|
import android.view.View;
|
||||||
|
import android.widget.Adapters;
|
||||||
|
import android.widget.TextView;
|
||||||
|
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.util.HashMap;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This custom cursor binder is used by the adapter defined in res/xml to
|
||||||
|
* bin contacts photos to their respective list item. This binder simply
|
||||||
|
* queries a contact's photo based on the contact's id and sets the
|
||||||
|
* photo as a compound drawable on the TextView used to display the contact's
|
||||||
|
* name.
|
||||||
|
*/
|
||||||
|
public class ContactPhotoBinder extends Adapters.CursorBinder {
|
||||||
|
private static final int PHOTO_SIZE_DIP = 54;
|
||||||
|
|
||||||
|
private final Drawable mDefault;
|
||||||
|
private final HashMap<Long, Drawable> mCache;
|
||||||
|
private final Resources mResources;
|
||||||
|
private final int mPhotoSize;
|
||||||
|
|
||||||
|
public ContactPhotoBinder(Context context, Adapters.CursorTransformation transformation) {
|
||||||
|
super(context, transformation);
|
||||||
|
|
||||||
|
mResources = mContext.getResources();
|
||||||
|
// Default picture used when a contact does not provide one
|
||||||
|
mDefault = mResources.getDrawable(R.drawable.ic_contact_picture);
|
||||||
|
// Cache used to avoid requerying contacts photos every time
|
||||||
|
mCache = new HashMap<Long, Drawable>();
|
||||||
|
// Compute the size of the photo based on the display's density
|
||||||
|
mPhotoSize = (int) (PHOTO_SIZE_DIP * mResources.getDisplayMetrics().density + 0.5f);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean bind(View view, Cursor cursor, int columnIndex) {
|
||||||
|
final long id = cursor.getLong(columnIndex);
|
||||||
|
|
||||||
|
// First check whether we have already cached the contact's photo
|
||||||
|
Drawable d = mCache.get(id);
|
||||||
|
|
||||||
|
if (d == null) {
|
||||||
|
// If the photo wasn't in the cache, ask the contacts provider for
|
||||||
|
// an input stream we can use to load the photo
|
||||||
|
Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, id);
|
||||||
|
InputStream stream = ContactsContract.Contacts.openContactPhotoInputStream(
|
||||||
|
mContext.getContentResolver(), uri);
|
||||||
|
|
||||||
|
// Creates the drawable for the contact's photo or use our fallback drawable
|
||||||
|
if (stream != null) {
|
||||||
|
Bitmap bitmap = BitmapFactory.decodeStream(stream);
|
||||||
|
d = new BitmapDrawable(mResources, bitmap);
|
||||||
|
} else {
|
||||||
|
d = mDefault;
|
||||||
|
}
|
||||||
|
|
||||||
|
d.setBounds(0, 0, mPhotoSize, mPhotoSize);
|
||||||
|
((TextView) view).setCompoundDrawables(d, null, null, null);
|
||||||
|
|
||||||
|
// Remember the photo associated with this contact
|
||||||
|
mCache.put(id, d);
|
||||||
|
} else {
|
||||||
|
((TextView) view).setCompoundDrawables(d, null, null, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2010 The Android Open Source Project
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.example.android.xmladapters;
|
||||||
|
|
||||||
|
import android.app.ListActivity;
|
||||||
|
import android.os.Bundle;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This activity demonstrates how to create a complex UI using a ListView
|
||||||
|
* and an adapter defined in XML.
|
||||||
|
*
|
||||||
|
* The following activity shows a list of contacts, their starred status
|
||||||
|
* and their photos, using the adapter defined in res/xml.
|
||||||
|
*/
|
||||||
|
public class ContactsListActivity extends ListActivity {
|
||||||
|
@Override
|
||||||
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
|
super.onCreate(savedInstanceState);
|
||||||
|
setContentView(R.layout.contacts_list);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user