实现对手机联系人列表进行读写操作,并用RecyclerView收缩展开方式展现
實現(xiàn)對手機(jī)聯(lián)系人列表進(jìn)行讀寫操作,并用RecyclerView收縮展開方式展現(xiàn)
在之前做的類微信界面上加了顯示手機(jī)聯(lián)系人,姓名,電話,郵箱三項信息的功能,同時可以添加聯(lián)系人同步到手機(jī)聯(lián)系人記錄中,添加完下拉刷新顯示。
完整的項目代碼
在Android中,如果想把A應(yīng)用的數(shù)據(jù)庫增加一個共享方式,就為A應(yīng)用寫一個ContentProvider方法,標(biāo)識B應(yīng)用要訪問的數(shù)據(jù)。然后通過ContentResolver的增刪改查方法實現(xiàn)對數(shù)據(jù)的共享操作。而讀寫手機(jī)聯(lián)系人的ContentResolver方法直接調(diào)用即可。
獲取數(shù)據(jù)存到自定義數(shù)據(jù)類型里的方法
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()){// 獲取查詢結(jié)果中E-mail地址列中數(shù)據(jù)String emailAddress = emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));dataBean.setChildRightTxt("Email:"+emailAddress);}emails.close();dataBeanList.add(dataBean);}cursor.close();}相比較之前寫的RecyclerView收縮展開靜態(tài)數(shù)據(jù)的方式,這次展現(xiàn)的數(shù)據(jù)是動態(tài)的。所以新建了個數(shù)據(jù)類,用于對手機(jī)聯(lián)系人姓名,號碼,郵箱的獲取/賦值。由于數(shù)據(jù)是展開,收縮的形式,便為一級列表和二級列表各寫了相應(yīng)的ViewHolder,兩者繼承相同的父類。
添加數(shù)據(jù)功能的思路,在點(diǎn)擊添加按鈕后出現(xiàn)一個彈框,用EditText接收輸入的信息。
final View view1 = getLayoutInflater().inflate(R.layout.addcontact, null);new AlertDialog.Builder(MainActivity.this).setTitle("添加手機(jī)聯(lián)系人信息").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();// 創(chuàng)建一個空的ContentValuesContentValues values = new ContentValues();// 向RawContacts.CONTENT_URI執(zhí)行一個空值插入// 目的是獲取系統(tǒng)返回的rawContactIdUri rawContactUri = getContentResolver().insert(ContactsContract.RawContacts.CONTENT_URI, values);long rawContactId = ContentUris.parseId(rawContactUri);values.clear();values.put(Data.RAW_CONTACT_ID, rawContactId);// 設(shè)置內(nèi)容類型values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);// 設(shè)置聯(lián)系人名字values.put(StructuredName.GIVEN_NAME, name);// 向聯(lián)系人URI添加聯(lián)系人名字getContentResolver().insert(ContactsContract.Data.CONTENT_URI, values);values.clear();values.put(Data.RAW_CONTACT_ID, rawContactId);values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);// 設(shè)置聯(lián)系人的電話號碼values.put(Phone.NUMBER, phone);// 設(shè)置電話類型values.put(Phone.TYPE, Phone.TYPE_MOBILE);// 向聯(lián)系人電話號碼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);// 設(shè)置聯(lián)系人的E-mail地址values.put(Email.DATA, email);// 設(shè)置該電子郵件的類型values.put(Email.TYPE, Email.TYPE_WORK);// 向聯(lián)系人E-mail URI添加E-mail數(shù)據(jù)getContentResolver().insert(ContactsContract.Data.CONTENT_URI, values);}}).show();最初沒有寫刷新功能,添加完后得重新進(jìn)入應(yīng)用才能顯示,于是參照刷新布局控件
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>刷新部分的實現(xiàn)
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);}});實現(xiàn)效果如下
這次遇到了大大小小很多錯誤,其中最令人深刻的是下面這個空指針異常的錯誤。
錯誤部分的代碼如下
最后找到了原因是我實例化EditText時,沒有加view1,導(dǎo)致我在處理這個錯誤上花了很多時間。最后查找了關(guān)于findViewById的解釋,才明白,findViewById默認(rèn)上下文是在Activity的主布局中,在子布局,如彈窗Dialog中,要用view.findViewById方法才行。
總結(jié)
以上是生活随笔為你收集整理的实现对手机联系人列表进行读写操作,并用RecyclerView收缩展开方式展现的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数据科学竞赛经验分享:你从未见过的究极进
- 下一篇: r5-5600H这颗芯片能满足日常的编程