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 android.app.ListActivity;
20 import android.database.Cursor;
21 import android.os.Bundle;
22 import android.provider.ContactsContract.CommonDataKinds.Phone;
23 import android.view.View;
24 import android.widget.SimpleCursorAdapter;
25 import android.widget.TextView;
26 
27  /**
28  * A list view example where the
29  * data comes from a cursor, and a
30  * SimpleCursorListAdapter is used to map each item to a two-line
31  * display.
32  */
33 public class List3 extends ListActivity {
34 
35     @Override
onCreate(Bundle savedInstanceState)36     protected void onCreate(Bundle savedInstanceState) {
37         super.onCreate(savedInstanceState);
38 
39         // Get a cursor with all phones
40         Cursor c = getContentResolver().query(Phone.CONTENT_URI,
41                 PHONE_PROJECTION, null, null, null);
42         startManagingCursor(c);
43 
44         // Map Cursor columns to views defined in simple_list_item_2.xml
45         SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
46                 android.R.layout.simple_list_item_2, c,
47                         new String[] {
48                             Phone.TYPE,
49                             Phone.NUMBER
50                         },
51                         new int[] { android.R.id.text1, android.R.id.text2 });
52         //Used to display a readable string for the phone type
53         adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
54             public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
55                 //Let the adapter handle the binding if the column is not TYPE
56                 if (columnIndex != COLUMN_TYPE) {
57                     return false;
58                 }
59                 int type = cursor.getInt(COLUMN_TYPE);
60                 String label = null;
61                 //Custom type? Then get the custom label
62                 if (type == Phone.TYPE_CUSTOM) {
63                     label = cursor.getString(COLUMN_LABEL);
64                 }
65                 //Get the readable string
66                 String text = (String) Phone.getTypeLabel(getResources(), type, label);
67                 //Set text
68                 ((TextView) view).setText(text);
69                 return true;
70             }
71         });
72         setListAdapter(adapter);
73     }
74 
75     private static final String[] PHONE_PROJECTION = new String[] {
76         Phone._ID,
77         Phone.TYPE,
78         Phone.LABEL,
79         Phone.NUMBER
80     };
81 
82     private static final int COLUMN_TYPE = 1;;
83     private static final int COLUMN_LABEL = 2;
84 }