android讀取通訊錄

來源:互聯網
上載者:User

標籤:arraylist   group   main   exce   ext   androi   xtend   mis   net   

第一步:註冊許可權

<uses-permission android:name="android.permission.WRITE_CONTACTS" /> 
<uses-permission android:name="android.permission.READ_CONTACTS" />

第二步:接收的model類

public class ContactModel {    private String name;    private ArrayList<String > list;    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public ArrayList<String> getList() {        return list;    }    public void setList(ArrayList<String> list) {        this.list = list;    }}

 第三步:mian——activity

public class MainActivity extends AppCompatActivity {    private Button button;    private ListView listView;    static final String TAG = "MainActivity";    ArrayList<ContactModel>dataList = new ArrayList<ContactModel>();    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        listView = (ListView)findViewById(R.id.contrcts_view);        readContacts();        MainAdapter adapter = new MainAdapter(this,dataList);        listView.setAdapter(adapter);    }    private void readContacts(){            Cursor cursor = null;           try {               cursor = this.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,                       null, null, null, null);               int contactIdIndex = 0;               int nameIndex = 0;               if(cursor.getCount() > 0) {                   contactIdIndex = cursor.getColumnIndex(ContactsContract.Contacts._ID);                   nameIndex = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);               }               ContactModel model = null;               while(cursor.moveToNext()) {                   model = new ContactModel();                   String contactId = cursor.getString(contactIdIndex);                   String name = cursor.getString(nameIndex);//                Log.i(TAG, contactId);                   Log.i(TAG, "==============:" + name);                   model.setName(name);            /*             * 尋找該連絡人的phone資訊             */                   Cursor phones = this.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,                           null,                           ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + contactId,                           null, null);                   int phoneIndex = 0;                   if (phones.getCount() > 0) {                       phoneIndex = phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);                   }                   ArrayList <String>list = new ArrayList<String>();                   while (phones.moveToNext()) {                       String phoneNumber = phones.getString(phoneIndex);                       Log.i(TAG, "==============:" + phoneNumber);                       list.add(phoneNumber);                   }                   model.setList(list);                   dataList.add(model);               }           }catch (Exception e){               Log.e(TAG,e.toString());           }finally {               if(cursor != null){                   cursor.close();               }           }    }}

 第四步:adapter

public class MainAdapter extends BaseAdapter {    private Context context;    private List<ContactModel> list;    public MainAdapter(Context context, List<ContactModel>list){        this.context = context;        this.list = list;    }    @Override    public int getCount() {        return this.list.size();    }    @Override    public Object getItem(int i) {        return this.list.get(i);    }    @Override    public long getItemId(int i) {        return i;    }    @Override    public View getView(int i, View view, ViewGroup viewGroup) {        ViewHolder holder;        if(view == null){            view = LayoutInflater.from(this.context).inflate(R.layout.item_main,null);            holder = new ViewHolder();            holder.nameTV = (TextView)view.findViewById(R.id.name);            holder.phoneTV = (TextView)view.findViewById(R.id.phone);            view.setTag(holder);        }else{            holder = (ViewHolder)view.getTag();        }        ContactModel model = this.list.get(i);        holder.nameTV.setText(model.getName());        StringBuffer buffer = new StringBuffer();       for (int j=0;j<model.getList().size();j++){           if(j==0){               buffer.append(model.getList().get(j));           }else {               buffer.append("\n"+model.getList().get(j));           }       }        holder.phoneTV.setText(buffer.toString());        return view;    }    class  ViewHolder{        TextView nameTV;        TextView phoneTV;    }}

 第五步:item 布局

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:orientation="vertical">    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="tom"        android:layout_margin="5dp"        android:id="@+id/name"/>    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="18580633453"        android:layout_margin="5dp"        android:id="@+id/phone"/>    <View        android:layout_width="match_parent"        android:layout_height="1dp"        android:background="#d6d6d6"        /></LinearLayout>

 

main的布局就一個listview,如果不會的話我需要靜靜了

android讀取通訊錄

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.