android取通訊錄電話號碼

tomivy 13年前發布 | 4K 次閱讀

今天在練習android應用開發揭密書中的例子時遇到了一個問題。例子4-5代碼不能工作,并且總是提示不存number這一列。

Caused by: java.lang.IllegalArgumentException: column 'number' does not exist

由于剛剛開始研究android,很多東西都沒搞清楚,所以先上網搜了一下,沒想到還有人也是遇到了一樣的問題,有人問但是沒有人回答,所以覺得得 好好研究一下。既然系統報的是找不到number這個列那么,就應該是在query時沒有搜出這一列數據。那么首先檢查query的參數:

Cursor cur = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);

根據android的開發文檔,我們得知這種方式是全列查訊,所以對這個問題來說有兩種可能:

1. 目標數據源中沒有number這列

2. 在其他代碼中出現了問題

再看下面的代碼

ListAdapter adapter = new SimpleCursorAdapter(this,android.R.layout.expandable_list_content,  

cur,new String[]{PhoneLookup.DISPLAY_NAME,PhoneLookup.NUMBER},

new int[]{android.R.id.text1,android.R.id.text2});

這段代碼沒有什么問題,因為只有有number列那就應該不會出錯,所以問題很可能是第一種情況。那么,就用調試器查看一下我們查詢出來的這個cursor中列集。

果 然,在列集中真的沒有number這一列。那么為什么在作者的例子中使用了這樣的code呢?我個人認為問題是這樣的,主要問題是number與 contact的對應關系上。事實上android在2.0之后有出來2.0.1版,這兩個版本對contact下的內容進行了一些改進,把number 這個屬性移動到了另一地方(Uri: ContactsContract.CommonDataKinds.Phone.CONTENT_URI)。所以,書里的代碼不能正常工作,這個結論是 我個人的猜測,還沒有仔細去比較版本的差別。

下面給出一個我改進過的,能在2.0.1SDK上工作的代碼:

代碼
  1 package example.moonzwu.code;
  2 
  3 import java.util.ArrayList;
  4 import java.util.HashMap;
  5 import java.util.Map;
  6 
  7 import android.app.Activity;
  8 import android.database.Cursor;
  9 import android.graphics.Color;
 10 import android.os.Bundle;
 11 import android.provider.ContactsContract;
 12 import android.view.View;
 13 import android.widget.AdapterView;
 14 import android.widget.LinearLayout;
 15 import android.widget.ListAdapter;
 16 import android.widget.ListView;
 17 import android.widget.SimpleAdapter;
 18 import android.widget.Toast;
 19 import android.widget.AdapterView.OnItemClickListener;
 20 import android.widget.AdapterView.OnItemSelectedListener;
 21 
 22 public class accessContact2 extends Activity {
 23     LinearLayout mLinLayout;
 24     ListView     mLstViw;
 25     ArrayList<Map<String, String>> listData;
 26     
 27     static final String NAME = "name";
 28     static final String NUMBER = "number";
 29     
 30     /** Called when the activity is first created. */
 31     @Override
 32     public void onCreate(Bundle savedInstanceState) {
 33         super.onCreate(savedInstanceState);
 34         mLinLayout = new LinearLayout(this);
 35         mLinLayout.setOrientation(LinearLayout.VERTICAL);
 36         mLinLayout.setBackgroundColor(Color.BLACK);
 37         
 38         mLstViw = new ListView(this);
 39         LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
 40                 LinearLayout.LayoutParams.FILL_PARENT, 
 41                 LinearLayout.LayoutParams.WRAP_CONTENT);
 42        mLstViw.setBackgroundColor(Color.BLACK);
 43        
 44        // add the list view to layout
 45        mLinLayout.addView(mLstViw, params);
 46        setContentView(mLinLayout);
 47        
 48        listData = new ArrayList<Map<String, String>>();
 49        
 50        
 51        // read contacts
 52        Cursor cur = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, 
 53                nullnullnullnull);
 54        startManagingCursor(cur);
 55        
 56        while (cur.moveToNext()) {
 57            Map<String, String> mp = new HashMap<String, String>();
 58            
 59            long id = cur.getLong(cur.getColumnIndex("_id"));
 60            Cursor pcur = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
 61                    null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + Long.toString(id), 
 62                    nullnull);
 63            
 64            // 處理多個號碼的情況
 65            String phoneNumbers = "";
 66            while (pcur.moveToNext()) {
 67                String strPhoneNumber = pcur.getString(pcur.getColumnIndex(
 68                        ContactsContract.CommonDataKinds.Phone.NUMBER));
 69                phoneNumbers += strPhoneNumber + ":";
 70            }
 71            phoneNumbers += "\n";
 72            pcur.close();
 73            
 74            String name = cur.getString(cur.getColumnIndex("display_name"));
 75            mp.put(NAME, name);
 76            mp.put(NUMBER, phoneNumbers);
 77            listData.add(mp);
 78         }
 79        cur.close();
 80        
 81        // 建立一個適配器去查詢數據
 82        ListAdapter adapter = new SimpleAdapter(this, listData, android.R.layout.simple_list_item_2,
 83                new String[]{NAME, NUMBER},
 84                new int[] {android.R.id.text1, android.R.id.text2});
 85        mLstViw.setAdapter(adapter);
 86 
 87        
 88        mLstViw.setOnItemClickListener(new OnItemClickListener() {
 89 
 90         @Override
 91         public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
 92                 long arg3) {
 93             // TODO Auto-generated method stub
 94             DisplayToast("選中第  " + Integer.toString(arg2+1+ "");
 95         }
 96        });
 97        
 98        
 99        mLstViw.setOnItemSelectedListener(new OnItemSelectedListener() {
100 
101         @Override
102         public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
103                 long arg3) {
104             // TODO Auto-generated method stub
105             DisplayToast("滾動到" + Long.toString(arg0.getSelectedItemId()) + "");
106         }
107 
108         @Override
109         public void onNothingSelected(AdapterView<?> arg0) {
110             // TODO Auto-generated method stub
111             
112         }
113            
114        });
115        
116     }
117     
118     public void DisplayToast(String str) {
119         Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
120     }
121 }

原文出處:
http://www.cnblogs.com/moonz-wu/archive/2010/06/28/1766404.html
</div></div>

 本文由用戶 tomivy 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!
寫博客

博客分類