如何从arraylist中删除重复的联系人 [英] how to remove duplicate contacts from arraylist

查看:89
本文介绍了如何从arraylist中删除重复的联系人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个应用,可以在其中从设备获取联系人. 但我想从结果中删除重复的联系人.

I have created an app in which I am getting the contacts from a device. But I want to remove the duplicate contacts from the results.

我该怎么办?

MainActivity

public class MainActivity extends Activity implements OnItemClickListener {

EditText searchText;

ArrayList<String> phno0 = new ArrayList<String>();
List<String> arrayListNames;
public List<ProfileBean> list;
public SearchableAdapter adapter;
//ProfileBean bean;
String[] cellArray = null;
String contacts;
ListView lv;
String phoneNumber, name;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ActionBar actionBar = getActionBar();


    lv = (ListView) findViewById(R.id.listview);
    list = new ArrayList<ProfileBean>();
    getAllCallLogs(this.getContentResolver());
    adapter = new SearchableAdapter(getApplication(), list);
    lv.setAdapter(adapter);
    lv.setItemsCanFocus(false);
    lv.setOnItemClickListener(this);
    lv.setTextFilterEnabled(true);


}

@Override
protected void onStart() {
    // TODO Auto-generated method stub
    super.onStart();

}

public void getAllCallLogs(ContentResolver cr) {

    Cursor phones = cr.query(
            ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,
            null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME
                    + " ASC");
    while (phones.moveToNext()) {
        phoneNumber = phones
                .getString(phones
                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
        name = phones
                .getString(phones
                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));

        list.add(new ProfileBean(name, phoneNumber));

    }
    phones.close();
}
}

推荐答案

在添加到列表之前,在list.add(new ProfileBean(name, phoneNumber));位置添加以下检查:

Add the following checking in the place of list.add(new ProfileBean(name, phoneNumber)); before adding into list:

int flag = 0
if(list.size() == 0){
list.add(new ProfileBean(name, phoneNumber));
}
    for(int i=0;i<list.size();i++){

    if(!list.get(i).getProfileName().trim().equals(name)){
    flag = 1;

    }else{
     flag =0;
     break;

}

    }
if(flag == 1){
list.add(new ProfileBean(name, phoneNumber));
}

这篇关于如何从arraylist中删除重复的联系人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆