Android-ContactPicker(选择联系人)详解

ContactPicker(选择联系人) 详解


网站建设哪家好,找成都创新互联公司!专注于网页设计、网站建设、微信开发、小程序开发、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了槐荫免费建站欢迎大家使用!

本文地址: http://blog.csdn.net/caroline_wendy/article/details/21629357


Android API: http://developer.android.com/reference/android/content/Intent.html


ContactPicker:用户可以选择一个联系人.


1. 创建ContactPicker活动(Activity)

位置: java->package-> ContactPicker.java

package mzx.spike.contactpicker.app;  import android.app.Activity; import android.content.ContentUris; import android.content.Intent; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.provider.ContactsContract; import android.view.View; import android.widget.AdapterView; import android.widget.ListView; import android.widget.SimpleCursorAdapter;  /**  * Created by C.L.Wang on 14-3-20.  */ public class ContactPicker extends Activity {      @Override     public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);          final Cursor c = getContentResolver().query(                 ContactsContract.Contacts.CONTENT_URI, null, null, null, null         );          String[] from = new String[]{ContactsContract.Contacts.DISPLAY_NAME_PRIMARY};         int[] to = new int[]{R.id.itemTextView};          SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.listitemlayout, c, from, to, 0);         ListView lv = (ListView)findViewById(R.id.contactListView);          lv.setAdapter(adapter);          lv.setOnItemClickListener(new ListView.OnItemClickListener(){             public void onItemClick(AdapterView parent, View view, int pos, long id) {                 c.moveToPosition(pos);                 int rowId = c.getInt(c.getColumnIndexOrThrow("_id"));                 Uri outURI = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, rowId);                 Intent outData = new Intent();                 outData.setData(outURI);                 setResult(Activity.RESULT_OK, outData);                 finish();             }         });     } } 

详解:

1.  创建一个活动继承Activity, 显示activity_main界面;

2. 创建游标Cursor遍历存储在联系人列表中的联系人;

3. 构造简单游标适配器(SimpleCursorAdapter), 注意此方法标记为遗弃, 后面需要加上参数0, 版本SDK11+;

4. 将适配器绑定至ListView, setAdapter();

5. 添加项目点击事件(setOnItemClickListener), 将游标移植点击位置, 取出rowID, 构造URI, 将URI绑定至Intent,;

6. 返回结果, setResult(), 结果码(Activity_RESULT_OK) 和 Intent;


2. 修改activity_main.xml

位置: res->layout->activity_main.xml

         

添加ListView, ContactPicker会调用此布局(layout);


3. 创建listitemlayout.xml

位置: res->layout->listitemlayout.xml

          

TextView显示ListView中的每一个联系人, ContactPicker会调用此布局(layout);


4. 创建测试布局contactpickertester.xml

位置: res->layout->contactpickertester.xml

              

TextView显示选择的联系人,Button选择按钮, 启动界面;


5. 创建测试类ContactPickerTester.java

位置: java->package->ContactPickerTester.java

package mzx.spike.contactpicker.app;  import android.app.Activity; import android.content.Intent; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.provider.ContactsContract; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.TextView;  /**  * Created by Administrator on 14-3-20.  */ public class ContactPickerTester extends Activity {      public static final int PICK_CONTACT = 1;      @Override     public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);          setContentView(R.layout.contactpickertester);         Button button = (Button)findViewById(R.id.pick_contact_button);          button.setOnClickListener(new View.OnClickListener() {             @Override             public void onClick(View view) {                 Intent intent = new Intent(Intent.ACTION_PICK, Uri.parse("content://contacts/"));                 startActivityForResult(intent, PICK_CONTACT);             }         });     }      @Override     public void onActivityResult(int reqCode, int resCode, Intent data) {         super.onActivityResult(reqCode, resCode, data);          switch (reqCode) {             case (PICK_CONTACT) : {                 if (resCode == Activity.RESULT_OK) {                     Uri contactData = data.getData();                     Cursor c = getContentResolver().query(contactData,null, null, null, null);                     c.moveToFirst();                     String name = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME_PRIMARY));                     c.close();                     TextView tv = (TextView)findViewById(R.id.selected_contact_textview);                     tv.setText(name);                 }             }             default: break;         }     }  } 

1. 在onCreate()方法内, 重写按键监听事件(button.setOnClickListene());

2. 在onClick()方法中, 指定Intent.ACTION_PICK动作, 和联系人数据库("content://contacts/");

3. 重写onActivityResult()事件, 处理动作返回值, reqCode, 请求码, resCode, 结果码,  data, Intent数据;

4. 提取数据, 更新ViewText的内容;


6. 修改Manifest, 进行注册

位置: root->AndroidManifest.xml

                                                                                                                                                                                                       

1. 注册ContactPicker, intent-filter, 动作PICK, 种类DEFAULT(默认动作);

2. 注册ContactPickerTester, 启动(MAIN)程序;


7. 运行程序


代码下载: http://download.csdn.net/detail/u012515223/7072781


Android - ContactPicker(选择联系人) 详解


网页名称:Android-ContactPicker(选择联系人)详解
文章链接:http://azwzsj.com/article/pgcdgh.html