網站首頁 編程語言 正文
本文實例為大家分享了Android實現手機聯系人分欄效果的具體代碼,供大家參考,具體內容如下
小編在項目時期遇見了制作手機聯系人分欄效果,查詢了很多資料,現在總結如下:
添加的代碼并不多,用ListView寫好數據以后,只需在Adapter里添加一個方法,并且在getView()方法里添加幾行代碼即可。不過小編現在介紹的方法,只適合做簡單項目,大型項目還沒研究該代碼是否有缺陷,歡迎各位大神批評指教。
給大家看一下,小編做的代碼效果圖:
adapter具體代碼如下:
public class ContactAdapter extends BaseAdapter{ ? ? ? private List contactBeens; ? ? ? private Context context; ? ? ? public ContactAdapter(Context context, List datas) { ? ? ? ? super(context, datas); ? ? ? ? this.context = context; ? ? ? ? contactBeens = datas; ? ? } ? @Override ? ? public View getView(int position, View convertView, ViewGroup parent) { ? ? ? ? ViewHodler viewHodler = null; ? ? ? ? if(convertView == null){ ? ? ? ? ? ? convertView = inflater.inflate(R.layout.contact_item,parent,false); ? ? ? ? ? ? viewHodler = new ViewHodler(convertView); ? ? ? ? ? ? convertView.setTag(viewHodler); ? ? ? ? }else { ? ? ? ? ? ? viewHodler = (ViewHodler)convertView.getTag(); ? ? ? ? } ? ? ? ? viewHodler.name.setText(contactBeens.get(position).getName()); ? ? ? ? viewHodler.number.setText(contactBeens.get(position).getNumber()); ? ? ? ? viewHodler.image.setmBitmap(Analysis(contactBeens.get(position).getImage())); ? ? ? ? // 獲得當前聯系人名字的首字母。 其中:getAlpha()方法是自己寫的一個方法(具體介紹如下), contactBeens.get(position).getAlpha()中的getAlpha()是我定義的實體類的get方法,值為當前聯系人的名字的拼音。 ? ? ? ? String currentStr = getAlpha(contactBeens.get(position).getAlpha()); ? ? ? ? // 獲得上一個聯系人名字的首字母 ? ? ? ? String previewStr = (position - 1) >= 0 ? getAlpha(contactBeens.get(position - 1).getAlpha()) : " "; ? ? ? ? /** ? ? ? ? ?* 判斷顯示#、A-Z的TextView隱藏與可見 ? ? ? ? ?*/ ? ? ? ? if (!previewStr.equals(currentStr)) { ? ? ? ? ? ? viewHodler.alpha.setVisibility(View.VISIBLE); ? ? ? ? ? ? viewHodler.alpha.setText(currentStr); ? ? ? ? } else { ? ? ? ? //當前聯系人與上一個聯系人首字母相同時,執行下面代碼,隱藏alpha(這是我定義的textView)。 ? ? ? ? ? ? viewHodler.alpha.setVisibility(View.GONE); ? ? ? ? } ? ? ? ? return convertView; ? ? } ? ? public class ViewHodler{ ? ? ? ? private ContomImage image; ? ? ? ? private TextView name; ? ? ? ? private TextView number; ? ? ? ? private TextView alpha; ? ? ? ? public ViewHodler(View view) { ? ? ? ? ? ? image = (ContomImage) view.findViewById(R.id.contact_contomImage); ? ? ? ? ? ? name = (TextView) view.findViewById(R.id.contact_name); ? ? ? ? ? ? number = (TextView) view.findViewById(R.id.contact_number); ? ? ? ? ? ? alpha = (TextView) view.findViewById(R.id.alpha); ? ? ? ? } ? ? } ? ? ?//通過聯系人的名字str ,返回聯系人名字的首字母大寫 ? ? @NonNull ? ? private String getAlpha(String str) { ? ? ? ? if (str == null) { ? ? ? ? ? ? return "#"; ? ? ? ? } ? ? ? ? if (str.trim().length() == 0) { ? ? ? ? ? ? return "#"; ? ? ? ? } ? ? ? ? char c = str.trim().substring(0, 1).charAt(0); ? ? ? ? //判斷首字母是否是英文字母 ? ? ? ? if (('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z')) { ? ? ? ? ? ? return (c + "").toUpperCase(); // 大寫輸出 ? ? ? ? } else { ? ? ? ? ? ? return "#"; ? ? ? ? } ? ? } }
適配器代碼解釋如下:
List contactBeens 數據,是從適配器的構造方法傳過來的。數據是根據Cursor一條一條的從虛擬機的聯系人數據庫讀出來的。
在適配器代碼中有多處出現getAlpha(),這個getAlpha()有兩個含義:
getAlpha(contactBeens.get(position).getAlpha()); 解釋: 從左邊開始,第一次出現的getAlpha()是代碼中最后定義的方法,是自己寫的一個方法。 第二次出現的getAlpha()是實體類里的get方法,
現在來看一下,ContactBeen這個實體類里的成員變量:
public class ContactBeen { ? ? private String name; ? ? private String number; ? ? private Uri image; ? ? //該變量存儲從虛擬機里讀出來的每個聯系人的名字的拼音 ? ? private String alpha; ? ? .... ?//剩下的為上述變量的get 和 ?set方法 以及該類的構造方法, 這里就不一一寫出來了。 }
到這里為止,已經實現了小編今天要說的手機聯系人分欄效果。如果有任何問題,給我留言,看見了一一答復,歡迎各位大神批評指教。
在這里,小編再給大家展示 : 如何從虛擬機中獲取手機聯系人的方法:
ContentResolver resolver1 = context.getContentResolver(); Cursor cursor1 ?= resolver1.query(uri,null,null,null,"sort_key"); ? if(cursor1 != null && cursor1.moveToFirst()){ ? ? ? ?int indexName = cursor1.getColumnIndex(Phone.DISPLAY_NAME); ? ? ? ?int indexNumber = cursor1.getColumnIndex(Phone.NUMBER); ? ? ? ?int indexId = cursor1.getColumnIndex(Phone.CONTACT_ID); ? ? ? ?int indexPhoneId = cursor1.getColumnIndex(Phone.PHOTO_ID); ? ? ? ?int indexAlpha = ?cursor1.getColumnIndex("sort_key");//"sort_key"保存的是聯系人名字的拼音字母 ? ? ? ? ? ? ? ? ? ? Uri uri1 = null; ? ? ? ? ? ? ? ? ? ? do { ? ? ? ? ? ? ? ? ? ? ? ? String name = cursor1.getString(indexName); ? ? ? ? ? ? ? ? ? ? ? ? String number = cursor1.getString(indexNumber); ? ? ? ? ? ? ? ? ? ? ? ? Long contactId = cursor1.getLong(indexId); ? ? ? ? ? ? ? ? ? ? ? ? Long phoneId = cursor1.getLong(indexPhoneId); ? ? ? ? ? ? ? ? ? ? ? ? String alpha = cursor1.getString(indexAlpha); ? ? ? ? ? ? ? ? ? ? ? ? if(phoneId > 0){ ? ? ? ? ? ? ? ? ? ? ? ? ? ? uri1 = ContentUris.withAppendedId(Contacts.CONTENT_URI,contactId); ? ? ? ? ? ? ? ? ? ? ? ? }else { ? ? ? ? ? ? ? ? ? ? ? ? ? ? uri1 = null ; ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ContactBeen been = new ContactBeen(name,number,uri1,alpha); ? ? ? ? ? ? ? ? ? ? ? ? Log.d(TAG, "name : "+ name + " number : " + number + " uri1 : " + uri1 + ?"alpha :" + alpha ); ? ? ? ? }while (cursor1.moveToNext()); ? ?cursor1.close(); }
原文鏈接:https://blog.csdn.net/qq_24697301/article/details/51997973
相關推薦
- 2022-03-30 C語言中static和auto用法詳解_C 語言
- 2022-09-14 C語言多媒體框架GStreamer入門和概述_C 語言
- 2022-04-09 IDEA中命令行安裝git報錯:error: failed to push some refs to
- 2022-04-16 C語言數據結構之二分法查找詳解_C 語言
- 2022-02-23 Proxy error Could not proxy request錯誤解決
- 2022-07-16 CSS 輪廓(outline)/CSS margin(外邊距)/CSS padding(填充)
- 2022-07-22 CSS設置元素隱藏的4種方法
- 2022-03-16 開發者必備Docker命令小結_docker
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支