1 /* 2 * Copyright (C) 2007 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.example.android.apis.view; 18 19 import com.example.android.apis.R; 20 21 import android.app.ListActivity; 22 import android.database.Cursor; 23 import android.os.Bundle; 24 import android.provider.ContactsContract; 25 import android.view.View; 26 import android.widget.AdapterView; 27 import android.widget.AdapterView.OnItemSelectedListener; 28 import android.widget.ListAdapter; 29 import android.widget.SimpleCursorAdapter; 30 import android.widget.TextView; 31 32 /** 33 * A list view example where the data comes from a cursor. 34 */ 35 public class List7 extends ListActivity implements OnItemSelectedListener { 36 private static final String[] PROJECTION = new String[] { 37 ContactsContract.Contacts._ID, 38 ContactsContract.Contacts.DISPLAY_NAME, 39 ContactsContract.Contacts.HAS_PHONE_NUMBER, 40 ContactsContract.Contacts.LOOKUP_KEY 41 }; 42 43 private int mIdColumnIndex; 44 private int mHasPhoneColumnIndex; 45 46 private TextView mPhone; 47 48 @Override onCreate(Bundle savedInstanceState)49 protected void onCreate(Bundle savedInstanceState) { 50 super.onCreate(savedInstanceState); 51 52 setContentView(R.layout.list_7); 53 54 mPhone = (TextView) findViewById(R.id.phone); 55 getListView().setOnItemSelectedListener(this); 56 57 // Get a cursor with all people 58 Cursor c = managedQuery(ContactsContract.Contacts.CONTENT_URI, 59 PROJECTION, null, null, null); 60 mIdColumnIndex = c.getColumnIndex(ContactsContract.Contacts._ID); 61 mHasPhoneColumnIndex = c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER); 62 63 ListAdapter adapter = new SimpleCursorAdapter(this, 64 android.R.layout.simple_list_item_1, // Use a template 65 // that displays a 66 // text view 67 c, // Give the cursor to the list adapter 68 new String[] { ContactsContract.Contacts.DISPLAY_NAME }, // Map the NAME column in the 69 // people database to... 70 new int[] { android.R.id.text1 }); // The "text1" view defined in 71 // the XML template 72 setListAdapter(adapter); 73 } 74 onItemSelected(AdapterView parent, View v, int position, long id)75 public void onItemSelected(AdapterView parent, View v, int position, long id) { 76 if (position >= 0) { 77 final Cursor c = (Cursor) parent.getItemAtPosition(position); 78 if (c.getInt(mHasPhoneColumnIndex) > 0) { 79 final long contactId = c.getLong(mIdColumnIndex); 80 final Cursor phones = getContentResolver().query( 81 ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 82 new String[] { ContactsContract.CommonDataKinds.Phone.NUMBER }, 83 ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + contactId, null, 84 ContactsContract.CommonDataKinds.Phone.IS_SUPER_PRIMARY + " DESC"); 85 86 try { 87 phones.moveToFirst(); 88 mPhone.setText(phones.getString(0)); 89 } finally { 90 phones.close(); 91 } 92 } else { 93 mPhone.setText(R.string.list_7_nothing); 94 } 95 } 96 } 97 onNothingSelected(AdapterView parent)98 public void onNothingSelected(AdapterView parent) { 99 mPhone.setText(R.string.list_7_nothing); 100 } 101 } 102