Home | History | Annotate | Download | only in view
      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 // Need the following import to get access to the app resources, since this
     20 // class is in a sub-package.
     21 import com.example.android.apis.R;
     22 
     23 
     24 import android.app.ListActivity;
     25 import android.database.Cursor;
     26 import android.provider.ContactsContract.CommonDataKinds.Phone;
     27 import android.os.Bundle;
     28 import android.view.View;
     29 import android.widget.AdapterView;
     30 import android.widget.AdapterView.OnItemSelectedListener;
     31 import android.widget.ListAdapter;
     32 import android.widget.SimpleCursorAdapter;
     33 import android.widget.TextView;
     34 
     35 /**
     36  * A list view example where the data comes from a cursor.
     37  */
     38 public class List7 extends ListActivity implements OnItemSelectedListener {
     39 
     40     private TextView mPhone;
     41 
     42     private static final String[] PHONE_PROJECTION = new String[] {
     43         Phone._ID,
     44         Phone.TYPE,
     45         Phone.LABEL,
     46         Phone.NUMBER,
     47         Phone.DISPLAY_NAME
     48     };
     49 
     50     private static final int COLUMN_PHONE_TYPE = 1;
     51     private static final int COLUMN_PHONE_LABEL = 2;
     52     private static final int COLUMN_PHONE_NUMBER = 3;
     53 
     54     @Override
     55     protected void onCreate(Bundle savedInstanceState) {
     56         super.onCreate(savedInstanceState);
     57         setContentView(R.layout.list_7);
     58         mPhone = (TextView) findViewById(R.id.phone);
     59         getListView().setOnItemSelectedListener(this);
     60 
     61         // Get a cursor with all numbers.
     62         // This query will only return contacts with phone numbers
     63         Cursor c = getContentResolver().query(Phone.CONTENT_URI,
     64                 PHONE_PROJECTION, Phone.NUMBER + " NOT NULL", null, null);
     65         startManagingCursor(c);
     66 
     67         ListAdapter adapter = new SimpleCursorAdapter(this,
     68                 // Use a template that displays a text view
     69                 android.R.layout.simple_list_item_1,
     70                 // Give the cursor to the list adapter
     71                 c,
     72                 // Map the DISPLAY_NAME column to...
     73                 new String[] {Phone.DISPLAY_NAME},
     74                 // The "text1" view defined in the XML template
     75                 new int[] {android.R.id.text1});
     76         setListAdapter(adapter);
     77     }
     78 
     79     public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
     80         if (position >= 0) {
     81             //Get current cursor
     82             Cursor c = (Cursor) parent.getItemAtPosition(position);
     83             int type = c.getInt(COLUMN_PHONE_TYPE);
     84             String phone = c.getString(COLUMN_PHONE_NUMBER);
     85             String label = null;
     86             //Custom type? Then get the custom label
     87             if (type == Phone.TYPE_CUSTOM) {
     88                 label = c.getString(COLUMN_PHONE_LABEL);
     89             }
     90             //Get the readable string
     91             String numberType = (String) Phone.getTypeLabel(getResources(), type, label);
     92             String text = numberType + ": " + phone;
     93             mPhone.setText(text);
     94         }
     95     }
     96 
     97     public void onNothingSelected(AdapterView<?> parent) {
     98     }
     99 }
    100