android連絡人搜尋

來源:互聯網
上載者:User

 

這幾天在完成《anbdroidSDK開發範例大全》中關於“搜尋手機通訊錄自動完成”的例子時,由於書上的例子是針對SDK1.5版本的,在用android2.2版本時,查詢到的手機號為null,避免大家走彎路,特將代碼貼出來共同探討學習,由於個人能力有限,望各位大蝦們指導。

建立autocontact.xml

   <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
  
<AutoCompleteTextView
   android:text=""
   android:id="@+id/contactACTV"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content">
</AutoCompleteTextView>
<TextView   android:text=""
   android:id="@+id/contactTV"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content">
</TextView>
</LinearLayout>

建立ContactActivity類

public class ContactActivity extends Activity{

//自動提示框
AutoCompleteTextView autoCompleteTextView;
TextView tv;
//資料庫查詢得到的遊標
Cursor cursor;

@Override
protected void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
  setContentView(R.layout.autocontacts);
  //獲得活動中的組件對象
  autoCompleteTextView = (AutoCompleteTextView)findViewById(R.id.contactACTV);
  tv = (TextView)findViewById(R.id.contactTV);  
  
  //查詢已存在的連絡人資訊 並將資訊綁定到autocompleteTextView中
  ContentResolver cr = getContentResolver();
  //制定查詢欄位
  String[] fieldes = {ContactsContract.Contacts._ID,ContactsContract.Contacts.DISPLAY_NAME};

  //獲得查詢的資訊
  cursor = cr.query(ContactsContract.Contacts.CONTENT_URI,
       fieldes, null, null, null);
  while(cursor.moveToNext())
  {
   String[] columns = cursor.getColumnNames();
   for(String s: columns)
   {
    String value = cursor.getString(cursor.getColumnIndexOrThrow(s));
    System.out.println(s+": "+value+"\n");
   }
  }
  
  //綁定自動提示框資訊
  ContentApdater adapter = new ContentApdater(this, cursor);
  autoCompleteTextView.setAdapter(adapter);
  
  autoCompleteTextView.setOnItemClickListener(new OnItemClickListener() {
  

   @Override
   public void onItemClick(AdapterView<?> parent, View view,
     int position, long id) {
    //將遊標定位到要顯示資料的行
    cursor.move(position);
    String name= cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
    String phone = showPhone();
    String info =name+"的\n \t\t電話是:"+phone;
   
    info += "\n \t\t電子郵件:"+showEmail();
    tv.setText(info);
   }
   
   
   
   //顯示電話號碼
   public String showPhone()
   {
    String id = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
    Cursor phoneCursor = getContentResolver().query
    (ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
      null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID+"=?", new String[]{id}, null);
   
    if(phoneCursor.moveToNext())
    {
     String phone =phoneCursor.getString(phoneCursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.DATA));
     return phone;
    }
    phoneCursor.close();
    return null;
   }
   //顯示電子郵件
   public String showEmail()
   {
    String id = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
    Cursor emailCursor = getContentResolver().query
    (ContactsContract.CommonDataKinds.Email.CONTENT_URI,
      null, ContactsContract.CommonDataKinds.Email.CONTACT_ID+"=?", new String[]{id}, null);
   
      if(emailCursor.moveToNext())
      {
       String email = emailCursor.getString(emailCursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Email.DATA));
       return email;
      }
    return null;
   }
   
  });
  
}
}
class ContentApdater extends CursorAdapter{

ContentResolver resolver;
//建構函式
public ContentApdater(Context context, Cursor c) {
  super(context, c);
  resolver  = context.getContentResolver();
}

@Override  //將資訊綁定到控制項的方法
public void bindView(View view, Context context, Cursor cursor) {
  ((TextView)view).setText(cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME)));
  
}
@Override
public CharSequence convertToString(Cursor cursor) {  
  return cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));  
}

@Override //建立自動綁定選項
public View newView(Context context, Cursor cursor, ViewGroup parent) {
  final LayoutInflater inflater = LayoutInflater.from(context);

  final TextView tv = (TextView)inflater.inflate(android.R.layout.simple_spinner_dropdown_item, parent,false);
  tv.setText(cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME)));
  return tv;
}
}

看效果時記得在功能選項中,建立連絡人(Contacts-->menu-->new contacts)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.