EX05_09.java
上传用户:vip_99
上传日期:2021-03-27
资源大小:61159k
文件大小:2k
源码类别:

android开发

开发平台:

Java

  1. package irdc.ex05_09;
  2. import android.app.Activity;
  3. import android.content.ContentResolver;
  4. import android.database.Cursor;
  5. import android.os.Bundle;
  6. import android.provider.Contacts;
  7. import android.view.View;
  8. import android.widget.AdapterView;
  9. import android.widget.AutoCompleteTextView;
  10. import android.widget.TextView;
  11. public class EX05_09 extends Activity
  12. {
  13.   private AutoCompleteTextView myAutoCompleteTextView;
  14.   private TextView myTextView1;
  15.   private Cursor contactCursor;
  16.   private ContactsAdapter myContactsAdapter;
  17.   /* 要捞出通讯录的字段 */
  18.   public static final String[] PEOPLE_PROJECTION = new String[]
  19.   { Contacts.People._ID, Contacts.People.PRIMARY_PHONE_ID,
  20.       Contacts.People.TYPE, Contacts.People.NUMBER, Contacts.People.LABEL,
  21.       Contacts.People.NAME };
  22.   /** Called when the activity is first created. */
  23.   @Override
  24.   public void onCreate(Bundle savedInstanceState)
  25.   {
  26.     super.onCreate(savedInstanceState);
  27.     setContentView(R.layout.main);
  28.     myAutoCompleteTextView = (AutoCompleteTextView)
  29.     findViewById(R.id.myAutoCompleteTextView);
  30.     myTextView1 = (TextView) findViewById(R.id.myTextView1);
  31.     /* 取得ContentResolver */
  32.     ContentResolver content = getContentResolver();
  33.     /* 取得通讯录的Cursor */
  34.     contactCursor = content.query
  35.     (
  36.       Contacts.People.CONTENT_URI,
  37.       PEOPLE_PROJECTION, null, null,
  38.       Contacts.People.DEFAULT_SORT_ORDER
  39.     );
  40.     /* 将Cursor传入自己实现的ContactsAdapter */
  41.     myContactsAdapter = new ContactsAdapter(this, contactCursor);
  42.     myAutoCompleteTextView.setAdapter(myContactsAdapter);
  43.     myAutoCompleteTextView.setOnItemClickListener
  44.     (new AdapterView.OnItemClickListener()
  45.     {
  46.       @Override
  47.       public void onItemClick
  48.       (AdapterView<?> arg0, View arg1, int arg2, long arg3)
  49.       {
  50.         /* 取得Cursor */
  51.         Cursor c = myContactsAdapter.getCursor();
  52.         /* 移到所点击的位置 */
  53.         c.moveToPosition(arg2);
  54.         String number = c.getString
  55.         (c.getColumnIndexOrThrow(Contacts.People.NUMBER));
  56.         /* 当找不到电话时显示无输入电话 */
  57.         number = number == null ? "无输入电话" : number;
  58.         myTextView1.setText(c.getString
  59.         (c.getColumnIndexOrThrow(Contacts.People.NAME))
  60.         + "的电话是" + number);
  61.       }
  62.     });  }
  63. }