实现对手机联系人列表进行读写操作,并用RecyclerView收缩展开方式展现
實現對手機聯系人列表進行讀寫操作,并用RecyclerView收縮展開方式展現
在之前做的類微信界面上加了顯示手機聯系人,姓名,電話,郵箱三項信息的功能,同時可以添加聯系人同步到手機聯系人記錄中,添加完下拉刷新顯示。
完整的項目代碼
在Android中,如果想把A應用的數據庫增加一個共享方式,就為A應用寫一個ContentProvider方法,標識B應用要訪問的數據。然后通過ContentResolver的增刪改查方法實現對數據的共享操作。而讀寫手機聯系人的ContentResolver方法直接調用即可。
獲取數據存到自定義數據類型里的方法
public void showlist(){dataBeanList = new ArrayList<>();Cursor cursor = getActivity().getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null,null,null,null);while(cursor.moveToNext()){String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));dataBean = new DataBean();dataBean.setID(contactId);dataBean.setType(0);dataBean.setChildBean(dataBean);dataBean.setParentLeftTxt("Name:"+name);Cursor phones = getActivity().getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID+"="+contactId,null,null);while(phones.moveToNext()){String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));dataBean.setChildLeftTxt("MobilePhone:"+phoneNumber);}phones.close();Cursor emails = getActivity().getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + contactId, null, null);while (emails.moveToNext()){// 獲取查詢結果中E-mail地址列中數據String emailAddress = emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));dataBean.setChildRightTxt("Email:"+emailAddress);}emails.close();dataBeanList.add(dataBean);}cursor.close();}相比較之前寫的RecyclerView收縮展開靜態數據的方式,這次展現的數據是動態的。所以新建了個數據類,用于對手機聯系人姓名,號碼,郵箱的獲取/賦值。由于數據是展開,收縮的形式,便為一級列表和二級列表各寫了相應的ViewHolder,兩者繼承相同的父類。
添加數據功能的思路,在點擊添加按鈕后出現一個彈框,用EditText接收輸入的信息。
final View view1 = getLayoutInflater().inflate(R.layout.addcontact, null);new AlertDialog.Builder(MainActivity.this).setTitle("添加手機聯系人信息").setView(view1).setPositiveButton("確定", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialogInterface, int which) {et_name = (EditText)view1.findViewById(R.id.et_name);et_phone = (EditText)view1.findViewById(R.id.et_phone);et_email=(EditText)view1.findViewById(R.id.et_email);String name = et_name.getText().toString();final String phone = et_phone.getText().toString();final String email = et_email.getText().toString();// 創建一個空的ContentValuesContentValues values = new ContentValues();// 向RawContacts.CONTENT_URI執行一個空值插入// 目的是獲取系統返回的rawContactIdUri rawContactUri = getContentResolver().insert(ContactsContract.RawContacts.CONTENT_URI, values);long rawContactId = ContentUris.parseId(rawContactUri);values.clear();values.put(Data.RAW_CONTACT_ID, rawContactId);// 設置內容類型values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);// 設置聯系人名字values.put(StructuredName.GIVEN_NAME, name);// 向聯系人URI添加聯系人名字getContentResolver().insert(ContactsContract.Data.CONTENT_URI, values);values.clear();values.put(Data.RAW_CONTACT_ID, rawContactId);values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);// 設置聯系人的電話號碼values.put(Phone.NUMBER, phone);// 設置電話類型values.put(Phone.TYPE, Phone.TYPE_MOBILE);// 向聯系人電話號碼URI添加電話號碼getContentResolver().insert(ContactsContract.Data.CONTENT_URI, values);values.clear();values.put(Data.RAW_CONTACT_ID, rawContactId);values.put(Data.MIMETYPE, Email.CONTENT_ITEM_TYPE);// 設置聯系人的E-mail地址values.put(Email.DATA, email);// 設置該電子郵件的類型values.put(Email.TYPE, Email.TYPE_WORK);// 向聯系人E-mail URI添加E-mail數據getContentResolver().insert(ContactsContract.Data.CONTENT_URI, values);}}).show();最初沒有寫刷新功能,添加完后得重新進入應用才能顯示,于是參照刷新布局控件
SwipeRefreshLayout的使用方法,加了個下拉刷新的功能。
布局文件
<android.support.v4.widget.SwipeRefreshLayoutandroid:id="@+id/srl"android:layout_width="match_parent"android:layout_height="match_parent"><android.support.v7.widget.RecyclerViewandroid:id="@+id/rcv_expandcollapse"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_margin="8dp"android:overScrollMode="never"android:scrollbars="none"/></android.support.v4.widget.SwipeRefreshLayout>刷新部分的實現
private SwipeRefreshLayout msrl;msrl = view.findViewById(R.id.srl);msrl.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {@Overridepublic void onRefresh() {new Handler().postDelayed(new Runnable() {@Overridepublic void run() {showlist();setData();msrl.setRefreshing(false);}},1200);}});實現效果如下
這次遇到了大大小小很多錯誤,其中最令人深刻的是下面這個空指針異常的錯誤。
錯誤部分的代碼如下
最后找到了原因是我實例化EditText時,沒有加view1,導致我在處理這個錯誤上花了很多時間。最后查找了關于findViewById的解釋,才明白,findViewById默認上下文是在Activity的主布局中,在子布局,如彈窗Dialog中,要用view.findViewById方法才行。
總結
以上是生活随笔為你收集整理的实现对手机联系人列表进行读写操作,并用RecyclerView收缩展开方式展现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数据科学竞赛经验分享:你从未见过的究极进
- 下一篇: r5-5600H这颗芯片能满足日常的编程