Android實(shí)現(xiàn)可瀏覽和搜索的聯(lián)系人列表
通過(guò)這篇文章,我想說(shuō)明一下如何創(chuàng)建一個(gè)可搜索的“聯(lián)系人列表”Android應(yīng)用程序。使用這個(gè)應(yīng)用程序,用戶(hù)可以通過(guò)使用導(dǎo)航按鈕瀏覽所有保存的聯(lián)系人和根據(jù)聯(lián)系人名稱(chēng)搜索聯(lián)系人。該應(yīng)用程序還可以顯示聯(lián)系人的照片(如果可用)。
要瀏覽聯(lián)系人列表可以使用<<,<,>和>>按鈕。
要搜索聯(lián)系人的用戶(hù)在“搜索名稱(chēng)”文本框中鍵入聯(lián)系人名稱(chēng),然后單擊“搜索”按鈕。點(diǎn)擊“清除搜索”按鈕,清除“搜索名稱(chēng)”文本框中,并顯示開(kāi)始搜索前,最后一次查看的聯(lián)系人。
由于該應(yīng)用從設(shè)備讀取聯(lián)系人,以下條目需要在AndroidManifest.xml文件,以允許權(quán)限應(yīng)用讀取聯(lián)系人:
<uses-permission android:name="android.permission.READ_CONTACTS"/>
下面的代碼創(chuàng)建一個(gè)表格布局顯示聯(lián)系人:
<TableLayout xmlns:android="<a rel="nofollow" target="_blank">http://schemas.android.com/apk/res/android"</a> android:layout_height="match_parent" android:layout_width="350dp"> <TableRow> <TextView android:id="@+id/txtId" android:width="175dp" android:text="Contact Id: "/> <TextView android:id="@+id/txtIdVal" android:width="175dp"/> </TableRow> <TableRow> <TextView android:id="@+id/txtDisplayName" android:width="175dp" android:text="Contact Name: "/> <TextView android:id="@+id/txtDisplayNameVal" android:width="175dp"/> </TableRow> <TableRow> <TextView android:id="@+id/txtPhoneNo" android:width="175dp" android:text="Phone Number: "/> <TextView android:id="@+id/txtPhoneNoVal" android:width="175dp"/> </TableRow> <TableRow> <TextView android:id="@+id/txtPhoto" android:width="175dp" android:text="Photo: "/> <ImageView android:id="@+id/imgPhoto" android:width="175dp"/> </TableRow> <TableRow> <Button android:id="@+id/btnFirst" android:width="175dp" android:text="<<" android:onClick="first"/> <Button android:id="@+id/btnPrevious" android:width="175dp" android:text="<" android:onClick="previous"/> </TableRow> <TableRow> <Button android:id="@+id/btnNext" android:width="175dp" android:text=">" android:onClick="next"/> <Button android:id="@+id/btnLast" android:width="175dp" android:text=">>" android:onClick="last"/> </TableRow> <TableRow> <TextView android:id="@+id/txtSearch" android:width="175dp" android:text="Search Name: "/> <AutoCompleteTextView android:id="@+id/txtSearchVal" android:width="175dp"/> </TableRow> <TableRow> <Button android:id="@+id/btnSearch" android:width="175dp" android:text="Search" android:onClick="search"/> <Button android:id="@+id/btnClearSearch" android:width="175dp" android:text="Clear Search" android:onClick="clearSearch"/> </TableRow> </TableLayout>
檢索圖像的地址,并使用以下命令訪問(wèn)聯(lián)系人:
Uri contacts=ContactsContract.Contacts.CONTENT_URI;
接下來(lái),我們創(chuàng)建一個(gè)CursorLoader對(duì)象按聯(lián)系人姓名的升序加載所有聯(lián)系人,如下:
CursorLoader loader=new
CursorLoader(this,contacts,null,null,null,ContactsContract.Contacts.DISPLAY_NAME+" asc");
該CursorLoader構(gòu)造采用下列參數(shù):
·Context context
·Uri uri
·String projection
·String selection
·String selectionArgs
·String sortOrder
下面的代碼將聯(lián)系人名稱(chēng)填充字符串?dāng)?shù)組中:
c=loader.loadInBackground();
names=new String<span>[</span>c.getCount()];
int ctr=0;
while(c.moveToNext())
{
names<span>[</span>ctr]=c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
ctr++;
}
在上面的代碼中,聯(lián)系人被加載到Cursor對(duì)象,該對(duì)象是一個(gè)使用loadInBackground()方法的CursorLoader類(lèi)。所有聯(lián)系人姓名存儲(chǔ)在一個(gè)字符串?dāng)?shù)組中,并使用Cursor類(lèi)中的MoveToNext()方法瀏覽所有聯(lián)系人。
此后一個(gè)ArrayAdapter被用于將聯(lián)系人姓名綁定到AutoCompleteTextView如下:
public void showContact(Cursor c)
{
String id=c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));
String displayName=c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
Bitmap photo;
InputStream stream=ContactsContract.Contacts.openContactPhotoInputStream
(getContentResolver(),ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI,Long.parseLong(id)));
if(stream!=null)
{
photo=BitmapFactory.decodeStream(stream);
imgPhoto.setImageBitmap(photo);
}
else
{
imgPhoto.setImageBitmap(null);
}
Cursor phoneCursor=getContentResolver().query
(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID+"="+id,null,null);
String number="";
if(phoneCursor.getCount()>0)
{
phoneCursor.moveToFirst();
number=phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
while(phoneCursor.moveToNext())
{
number+=","+phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
}
phoneCursor.close();
txtIdVal.setText(id);
txtDisplayNameVal.setText(displayName);
txtPhoneNoVal.setText(number);
enableDisableButtons();
}
上面的代碼使用cursor參數(shù)獲取聯(lián)系人的ID,顯示姓名,照片和電話(huà)號(hào)碼。它使用openContactPhotoInputStream()方法來(lái)為照片返回輸入流和decodeStream()方法來(lái)讀取照片。然后,它使用的setImageBitmap()方法在ImageView上顯示聯(lián)系人照片。當(dāng)信息存儲(chǔ)在另一個(gè)表時(shí),為了顯示電話(huà)號(hào)碼我們必須使用另一查詢(xún)。
以下代碼啟用和禁用基于所述查詢(xún)結(jié)果的導(dǎo)航按鈕:
public void enableDisableButtons()
{
if(c.isFirst()&&c.isLast())
{
btnFirst.setEnabled(false);
btnPrevious.setEnabled(false);
btnNext.setEnabled(false);
btnLast.setEnabled(false);
}
else if(c.isFirst())
{
btnFirst.setEnabled(false);
btnPrevious.setEnabled(false);
btnNext.setEnabled(true);
btnLast.setEnabled(true);
}
else if(c.isLast())
{
btnFirst.setEnabled(true);
btnPrevious.setEnabled(true);
btnNext.setEnabled(false);
btnLast.setEnabled(false);
}
else
{
btnFirst.setEnabled(true);
btnPrevious.setEnabled(true);
btnNext.setEnabled(true);
btnLast.setEnabled(true);
}
}
點(diǎn)擊搜索按鈕允許你基于名稱(chēng)在搜索文本框中搜索聯(lián)系方式,如下:
public void search(View v)
{
position=c.getPosition();
if(txtSearchVal.getText().toString().trim().length()>0)
{
Uri contacts=ContactsContract.Contacts.CONTENT_URI;
CursorLoader loader=new CursorLoader
(this,contacts,null,ContactsContract.Contacts.DISPLAY_NAME+"='"+txtSearchVal.getText().toString()+"'",null,
ContactsContract.Contacts.DISPLAY_NAME+" asc");
c=loader.loadInBackground();
if(c.getCount()>0)
{
c.moveToFirst();
}
}
else
{
Uri contacts=ContactsContract.Contacts.CONTENT_URI;
CursorLoader loader=new CursorLoader
(this,contacts,null,null,null,ContactsContract.Contacts.DISPLAY_NAME+" asc");
c=loader.loadInBackground();
c.move(position);
c.moveToNext();
}
if(c.getCount()==0)
{
Toast.makeText(this,"No results found for contact "+txtSearchVal.getText().toString(),Toast.LENGTH_SHORT).show();
showAll();
return;
}
showContact(c);
}
上面的代碼實(shí)現(xiàn)了通過(guò)聯(lián)系人姓名找到聯(lián)系方式功能。
點(diǎn)擊清除搜索文本框執(zhí)行下面的代碼:
public void clearSearch(View View)
{
showAll();
txtSearchVal.setText("");
}
showAll()方法顯示所有聯(lián)系人,如下:
public void showAll()
{
Uri contacts=ContactsContract.Contacts.CONTENT_URI;
CursorLoader loader=new CursorLoader(this,contacts,null,null,null,ContactsContract.Contacts.DISPLAY_NAME+" asc");
c=loader.loadInBackground();
c.move(position);
c.moveToNext();
showContact(c);
}
下面的代碼可以使用導(dǎo)航按鈕導(dǎo)航:
public void first(View v)
{
c.moveToFirst();
showContact(c);
}
public void previous(View v)
{
c.moveToPrevious();
showContact(c);
}
public void next(View v)
{
c.moveToNext();
showContact(c);
}
public void last(View v)
{
c.moveToLast();
showContact(c);
}
效果圖:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android自定義View實(shí)現(xiàn)可以拖拽的GridView
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)可以拖拽的GridView,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-06-06
android實(shí)現(xiàn)點(diǎn)擊圖片全屏展示效果
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)點(diǎn)擊圖片全屏展示效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-08-08
Android實(shí)現(xiàn)仿今日頭條點(diǎn)贊動(dòng)畫(huà)效果實(shí)例
我想看到今日頭條的點(diǎn)贊效果,應(yīng)該都覺(jué)得很絢麗吧,下面這篇文章主要給大家介紹了關(guān)于Android實(shí)現(xiàn)仿今日頭條點(diǎn)贊動(dòng)畫(huà)效果的相關(guān)資料,文中通過(guò)示例代價(jià)介紹的非常詳細(xì),需要的朋友可以參考下2022-02-02
android 監(jiān)聽(tīng)網(wǎng)絡(luò)狀態(tài)的變化及實(shí)戰(zhàn)的示例代碼
本篇文章主要介紹了android 監(jiān)聽(tīng)網(wǎng)絡(luò)狀態(tài)的變化及實(shí)戰(zhàn)的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-01-01
Android編程實(shí)現(xiàn)打勾顯示輸入密碼功能
這篇文章主要介紹了Android編程實(shí)現(xiàn)打勾顯示輸入密碼功能,涉及Android控件布局及屬性相關(guān)操作技巧,需要的朋友可以參考下2017-02-02
Android的App啟動(dòng)時(shí)白屏的問(wèn)題解決辦法
這篇文章主要介紹了Android的App啟動(dòng)時(shí)白屏的問(wèn)題相關(guān)資料,在App啟動(dòng)的第一次的時(shí)候白屏?xí)欢螘r(shí)間,這里提供了解決辦法,需要的朋友可以參考下2017-08-08
詳解Android Studio中Git的配置及協(xié)同開(kāi)發(fā)
這篇文章主要介紹了詳解Android Studio中Git的配置及協(xié)同開(kāi)發(fā),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-03-03
Android開(kāi)發(fā)之進(jìn)度條ProgressBar的示例代碼
本篇文章主要介紹了Android開(kāi)發(fā)之進(jìn)度條ProgressBar的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-03-03

