如何从数组列表中删除重复的联系人

2023-12-28

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

我怎样才能做到呢?

主要活动

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

  • 如果您想删除重复项,请考虑使用HashSet反而。

  • 如果您不能/不想使用它,只需在添加之前检查该联系人是否已经存在。

    if (!myList.contains(newContact))
      myList.add(newContact);
    
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何从数组列表中删除重复的联系人 的相关文章

随机推荐