From f4ff28edda30f72984eeb50a9153879c1e5f0abb Mon Sep 17 00:00:00 2001 From: Romain Guy Date: Thu, 10 Dec 2009 15:19:03 -0800 Subject: [PATCH] Cleanup one of the ListView sample demos. --- samples/ApiDemos/res/layout/list_7.xml | 4 +- .../com/example/android/apis/view/List7.java | 64 ++++++++++++------- 2 files changed, 44 insertions(+), 24 deletions(-) diff --git a/samples/ApiDemos/res/layout/list_7.xml b/samples/ApiDemos/res/layout/list_7.xml index 2dc777707..dfaf093e7 100644 --- a/samples/ApiDemos/res/layout/list_7.xml +++ b/samples/ApiDemos/res/layout/list_7.xml @@ -20,7 +20,7 @@ android:layout_height="fill_parent" android:paddingLeft="8dip" android:paddingRight="8dip"> - + - + diff --git a/samples/ApiDemos/src/com/example/android/apis/view/List7.java b/samples/ApiDemos/src/com/example/android/apis/view/List7.java index d44ed5672..e773db62a 100644 --- a/samples/ApiDemos/src/com/example/android/apis/view/List7.java +++ b/samples/ApiDemos/src/com/example/android/apis/view/List7.java @@ -16,15 +16,12 @@ package com.example.android.apis.view; -// Need the following import to get access to the app resources, since this -// class is in a sub-package. import com.example.android.apis.R; - import android.app.ListActivity; import android.database.Cursor; -import android.provider.Contacts.People; import android.os.Bundle; +import android.provider.ContactsContract; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemSelectedListener; @@ -36,46 +33,69 @@ import android.widget.TextView; * A list view example where the data comes from a cursor. */ public class List7 extends ListActivity implements OnItemSelectedListener { - private static String[] PROJECTION = new String[] { - People._ID, People.NAME, People.NUMBER + private static final String[] PROJECTION = new String[] { + ContactsContract.Contacts._ID, + ContactsContract.Contacts.DISPLAY_NAME, + ContactsContract.Contacts.HAS_PHONE_NUMBER, + ContactsContract.Contacts.LOOKUP_KEY }; + private int mIdColumnIndex; + private int mHasPhoneColumnIndex; + + private TextView mPhone; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + setContentView(R.layout.list_7); + mPhone = (TextView) findViewById(R.id.phone); getListView().setOnItemSelectedListener(this); // Get a cursor with all people - Cursor c = getContentResolver().query(People.CONTENT_URI, PROJECTION, null, null, null); - startManagingCursor(c); - mPhoneColumnIndex = c.getColumnIndex(People.NUMBER); + Cursor c = managedQuery(ContactsContract.Contacts.CONTENT_URI, + PROJECTION, null, null, null); + mIdColumnIndex = c.getColumnIndex(ContactsContract.Contacts._ID); + mHasPhoneColumnIndex = c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER); ListAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, // Use a template - // that displays a - // text view - c, // Give the cursor to the list adatper - new String[] {People.NAME}, // Map the NAME column in the - // people database to... - new int[] {android.R.id.text1}); // The "text1" view defined in - // the XML template + // that displays a + // text view + c, // Give the cursor to the list adapter + new String[] { ContactsContract.Contacts.DISPLAY_NAME }, // Map the NAME column in the + // people database to... + new int[] { android.R.id.text1 }); // The "text1" view defined in + // the XML template setListAdapter(adapter); } public void onItemSelected(AdapterView parent, View v, int position, long id) { if (position >= 0) { - Cursor c = (Cursor) parent.getItemAtPosition(position); - mPhone.setText(c.getString(mPhoneColumnIndex)); + final Cursor c = (Cursor) parent.getItemAtPosition(position); + if (c.getInt(mHasPhoneColumnIndex) > 0) { + final long contactId = c.getLong(mIdColumnIndex); + final Cursor phones = getContentResolver().query( + ContactsContract.CommonDataKinds.Phone.CONTENT_URI, + new String[] { ContactsContract.CommonDataKinds.Phone.NUMBER }, + ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + contactId, null, + ContactsContract.CommonDataKinds.Phone.IS_SUPER_PRIMARY + " DESC"); + + try { + phones.moveToFirst(); + mPhone.setText(phones.getString(0)); + } finally { + phones.close(); + } + } else { + mPhone.setText(R.string.list_7_nothing); + } } } public void onNothingSelected(AdapterView parent) { mPhone.setText(R.string.list_7_nothing); - } - - private int mPhoneColumnIndex; - private TextView mPhone; }