欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android實現(xiàn)手機聯(lián)系人分欄效果

 更新時間:2022年03月21日 14:44:48   作者:_陌陌  
這篇文章主要為大家詳細介紹了Android實現(xiàn)手機聯(lián)系人分欄效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Android實現(xiàn)手機聯(lián)系人分欄效果的具體代碼,供大家參考,具體內(nèi)容如下

小編在項目時期遇見了制作手機聯(lián)系人分欄效果,查詢了很多資料,現(xiàn)在總結(jié)如下:

添加的代碼并不多,用ListView寫好數(shù)據(jù)以后,只需在Adapter里添加一個方法,并且在getView()方法里添加幾行代碼即可。不過小編現(xiàn)在介紹的方法,只適合做簡單項目,大型項目還沒研究該代碼是否有缺陷,歡迎各位大神批評指教。

給大家看一下,小編做的代碼效果圖:

adapter具體代碼如下:

public class ContactAdapter extends BaseAdapter<ContactBeen>{
? ? ? private List<ContactBeen> contactBeens;
? ? ? private Context context;

? ? ? public ContactAdapter(Context context, List<ContactBeen> 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()));

? ? ? ? // 獲得當前聯(lián)系人名字的首字母。 其中:getAlpha()方法是自己寫的一個方法(具體介紹如下), contactBeens.get(position).getAlpha()中的getAlpha()是我定義的實體類的get方法,值為當前聯(lián)系人的名字的拼音。
? ? ? ? String currentStr = getAlpha(contactBeens.get(position).getAlpha());
? ? ? ? // 獲得上一個聯(lián)系人名字的首字母
? ? ? ? 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 {
? ? ? ? //當前聯(lián)系人與上一個聯(lián)系人首字母相同時,執(zhí)行下面代碼,隱藏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);
? ? ? ? }
? ? }

? ? ?//通過聯(lián)系人的名字str ,返回聯(lián)系人名字的首字母大寫
? ? @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 數(shù)據(jù),是從適配器的構(gòu)造方法傳過來的。數(shù)據(jù)是根據(jù)Cursor一條一條的從虛擬機的聯(lián)系人數(shù)據(jù)庫讀出來的。
在適配器代碼中有多處出現(xiàn)getAlpha(),這個getAlpha()有兩個含義:

getAlpha(contactBeens.get(position).getAlpha()); 解釋: 從左邊開始,第一次出現(xiàn)的getAlpha()是代碼中最后定義的方法,是自己寫的一個方法。 第二次出現(xiàn)的getAlpha()是實體類里的get方法,

現(xiàn)在來看一下,ContactBeen這個實體類里的成員變量:

public class ContactBeen {

? ? private String name;
? ? private String number;
? ? private Uri image;
? ? //該變量存儲從虛擬機里讀出來的每個聯(lián)系人的名字的拼音
? ? private String alpha;

? ? .... ?//剩下的為上述變量的get 和 ?set方法 以及該類的構(gòu)造方法, 這里就不一一寫出來了。
}

到這里為止,已經(jīng)實現(xiàn)了小編今天要說的手機聯(lián)系人分欄效果。如果有任何問題,給我留言,看見了一一答復,歡迎各位大神批評指教。

在這里,小編再給大家展示 : 如何從虛擬機中獲取手機聯(lián)系人的方法:

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"保存的是聯(lián)系人名字的拼音字母

? ? ? ? ? ? ? ? ? ? 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();
}

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

您可能感興趣的文章:

相關(guān)文章

最新評論