Android - Remove Duplicate Contacts - android

I am trying to display contacts in Recycler View everything is working fine but contacts are getting displayed twice or thrice.
Here is the Code
RecyclerView recyclerView;
List<Contacts> contactsList;
ContactsAdapter adapter;
public void getContactList() {
Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null, null, null,
"UPPER(" + ContactsContract.Contacts.DISPLAY_NAME + ") ASC");
while (cursor.moveToNext()) {
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Contacts contacts = new Contacts(name, number);
if (contactsList.contains(contacts)){
cursor.moveToNext();
}
else {
contactsList.add(contacts);
}
adapter = new ContactsAdapter(contactsList, getApplicationContext());
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
}
So, how can I solve this problem?

Pass only phoneNumber as a key and phoneName as a value
You can use :
Map<String, String> map = new HashMap<>();
map.put("A", "1");
...
System.out.printf("Before: %s%n", map);
// Set in which we keep the existing values
Set<String> existing = new HashSet<>();
map = map.entrySet()
.stream()
.filter(entry -> existing.add(entry.getValue()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
System.out.printf("After: %s%n", map);
for example :
Before: {A=1, B=2, C=2, D=3, E=3} After: {A=1, B=2, D=3}

You can change List with HashSet. HashSet prevents to have duplicate items. Your final code should be like that:
RecyclerView recyclerView;
HashSet<Contacts> contactsSet;
ContactsAdapter adapter;
public void getContactList() {
Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null, null, null,
"UPPER(" + ContactsContract.Contacts.DISPLAY_NAME + ") ASC");
while (cursor.moveToNext()) {
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Contacts contacts = new Contacts(name, number);
contactsSet.add(contacts);
}
List<Contacts> contactsList = new ArrayList<>();
adapter = new ContactsAdapter(contactsList, getApplicationContext());
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}

I think there are a couple of questions you should think about. Is the query wrong or is the way I save contacts wrong?
In your app should contacts be allowed to be duplicated? If no you could add a unique constraint to your migration. See Sql Lite Unique Constraints
If your DB should be accepting duplicate values, then you should add a column that signifies the unique ones to that user? Ie userId column or a place the contact is associated with and then add that into your query as Selection and SelectionArgs.
Lastly if you are just looking for a quick fix... you should dive deeper into your filtering.
Your code contactsList.contains(contacts) will never be true because you are always creating the contact on the line above. Then you are checking if your list contains that newly created one. Otherwise you can use the above solution https://stackoverflow.com/a/64535787/5398130

You can override equals() method in class Contacts. This way your contactList.contains(Contact c) can evaluate to true if two contact objects are logically equals from your override method, and thus it will not add it to the list again.
#Override
public boolean equals(Object o){
if(!o.getClass().equals(Contacts.class)) return false;
Contact c2 = (Contact)o;
// if phonenumber is unique to differentiate contact
return c2.number.equals(number);
}

You can follow these steps on this page where i can provide the full solution here is the link

Related

Sorted list of contacts having duplicates ,why?

I have sorted and listed my phone contacts in to an arraylist but ,i got many duplicates of same contact names in the list .How this happens? how to avoid this?
This is what i have tried,
cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null,
"(" + ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + ") ASC");
while (cursor.moveToNext()) {
try {
name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
phonenumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
contact_names_list.add(name);
phone_num_list.add(phonenumber);
} catch (Exception e) {
e.printStackTrace();
}
can anyone help??
No one here seems to answer your question.
The reason you're seeing duplicate contacts is that you're querying for phones not contacts.
In Android there are 3 main tables:
Contacts table - has one item per contact
RawContacts table - has one item per-contact per-account (such as Google, Outlook, Whatsapp, etc.) - multiple RawContacts are linked to a single Contact
Data table - has one item per detail (name, email, phone, address, etc.) - each data item is linked to a single RawContact, and multiple Data rows are linked to each RawContact.
You're querying on CommonDataKinds.Phone.CONTENT_URI which is a part of the Data table, so if a contact has more then one phone, and/or it has the same phone from multiple sources (e.g. Google and Whatsapp) you'll get the same phone with the same CONTACT_ID more then once.
The solution would be, to use a HashMap (rather then a HashSet), where the key is CONTACT_ID, so you can display multiple phones per contact:
String[] projection = new String[] { CommonDataKinds.Phone.CONTACT_ID, CommonDataKinds.Phone.DISPLAY_NAME, CommonDataKinds.Phone.NUMBER };
Cursor cursor = getContentResolver().query(CommonDataKinds.Phone.CONTENT_URI, projection, null, null, null);
HashMap<Long, Contact> contacts = new HashMap<>();
while (cursor.moveToNext()) {
long id = cursor.getLong(0);
String name = cursor.getString(1);
String phone = cursor.getString(2);
Contact c = contacts.get(id);
if (c == null) {
// newly found contact, add to Map
c = new Contact();
c.name = name;
contacts.put(id, c);
}
// add phone to contact class
c.phones.add(phone);
}
cursor.close();
// simple class to store multiple phones per contact
private class Contact {
public String name;
// use can use a HashSet here to avoid duplicate phones per contact
public List<String> phones = new ArrayList<>();
}
If you want to sort your HashMap by name:
List<Contact> values = new ArrayList<>(contacts.values());
Collections.sort(values, new Comparator<Contact> {
public int compare(Contact a, Contact b) {
return a.name.compareTo(b.name);
}
});
// iterate the sorted list, per contact:
for (Contact contact : values) {
Log.i(TAG, "contact " + contact.name + ": ");
// iterate the list of phones within each contact:
for (String phone : contact.phones) {
Log.i(TAG, "\t phone: " + phone);
}
}
You can try with HashSet.
public class HashSet extends AbstractSet implements Set,
Cloneable, Serializable
Duplicate values are not allowed.
Code Structure
HashSet<String> hashSET = new HashSet<String>();
hashSET.add("AA");
hashSET.add("BB");
hashSET.add("CC");
hashSET.add("AA"); // Adding duplicate elements
Then
Iterator<String> j = hashSET.iterator();
while (j.hasNext())
System.out.println(j.next()); // Will print "AA" once.
}
Now SORT your Hashset Values using TreeSet.
TreeSet implements the SortedSet interface so duplicate values are not
allowed.
TreeSet<String> _treeSET= new TreeSet<String>(hashSET);
May be in your contacts having multiple groups, and that group will be a WhatsApp,Google etc..Go to your contacts and search that contact having whatsApp account. will showing double entry with different Group
you should use or change your ContactsBean , in your Bean use HashSet
Note: HashSet can avoid duplicate entry more
HashSet contains unique elements only,it can avoid same key element form HashSet
Example bean
public class ContactBean {
private HashSet<String> number = new HashSet<String>();
public void setNumber(String number) {
if (number == null)
return;
this.number.add(number.trim());
}
public HashSet<String> getNumber() {
return this.number;
}
}
Simple Example
//Creating HashSet and adding elements
HashSet<String> hashSet=new HashSet<String>();
hashSet.add("Dhruv");
hashSet.add("Akash");
hashSet.add("Dhruv"); //Avoiding this entry
hashSet.add("Nirmal");
//Traversing elements
Iterator<String> itr = hashSet.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}
You can use HashSet for avoid duplication:-
HashSet<String> hset =
new HashSet<String>();
you can add like ArrayList in HashSet:-
hset.add(your_string);
OR
Convert your ArrayList to HashSet :-
Set<String> set = new HashSet<String>(your_arraylist_object);
HashSet avoid Duplicate Entry :)
I don't know why are you getting duplicate items from contacts, maybe phone contacts already have duplicate values.You can check that in Contacts app.
You should always use set data structure wherever you want to avoid duplicate items. You can find the better explanation and example here.
I think your duplication is because of Whatsapp contact interfering with contact . so you can use something like this
String lastPhoneName = "";
String lastPhoneNumber = "";
//IN YOUR CONTACT FETCHING WHILE LOOP , INSIDE TRY
String contactName = c.getString(c
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phNumber = c
.getString(c
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
if (!contactName.equalsIgnoreCase(lastPhoneName) && !phNumber.equalsIgnoreCase(lastPhoneNumber)) {
lastPhoneName = contactName;
lastPhoneNumber = phNumber;
ContactModel model = new ContactModel();
model.setContact_id(contactid);
model.setContact_name(contactName.replaceAll("\\s+", ""));
model.setContact_number(phNumber.replaceAll("\\D+", ""));
list_contact_model.add(model);
}
this will check that previous number is same as old one than skip it . I hope You get your answer
HashSet add items in key/value pair and also remove duplicate entry from item set.
List<String> phone_num_list= new ArrayList<>();
// add elements to phone_num_list, including duplicates
Set<String> hs = new HashSet<>();
hs.addAll(phone_num_list);
phone_num_list.clear();
phone_num_list.addAll(hs);
Happy coding!!

Query Database with an array list and get an array list of results back

I have setup an application which currently can lookup an input id with one on the database to then give a single result. E.g. user enters id = 1 , database contains a record with an id of 1 then returns the name or number etc...
Now I want to improve the system slightly by querying my database with an arraylist which contains a range of id's e.g. 3, 456, 731 etc... which I want my database to search for. I have also grouped multiple values to certain id's for example the database might search for an id of 3 it will then find 5 results I want it to return the telephone number of each one of those results into another arraylist which I can print to the logs.
I hope I have explained this enough, but please ask questions if you require more information.
The code below demonstrates the modified version of the query used to gain a single result, but I cannot see what I'm doing wrong to gain multiple results.
Activity....
// New array list which is going to be used to store values from the database
ArrayList<String> contactsList;
// This arrayList has been received from another activity and contains my id's
ArrayList<String> contacts = intent.getStringArrayListExtra("groupCode");
// The database which i'm using
ContactDBHandler contactDBHandler = new ContactDBHandler(getApplicationContext(), null, null, 1);
//getAllValues is used to pass my arraylist id's to the database.
contactsList = contactDBHandler.GetAllValues(contacts);
// Simple log statement to loop and display results
for (int i = 0; i < contactsList.size(); i++){
Log.i("Numbers", contactsList.get(i));
}
ContactDBHandler
Query
// I'm telling it to get the contact number from the contact_list
// when the groupcode matches the code recieved.
public ArrayList<String> GetAllValues(ArrayList groupCode)
{
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = null;
String alarmName = "";
ArrayList<String> list = new ArrayList<>();
cursor = db.rawQuery("SELECT contact_number FROM contact_list WHERE grp_code=?", new String[]{groupCode+ ""});
if (cursor.moveToFirst())
{
do
{
list.add(cursor.getString(0));
}
while (cursor.moveToNext());
}
if (cursor != null && !cursor.isClosed())
{
cursor.close();
}
return list;
}
Thanks
Can you see where I have gone wrong?
Try this:
cursor = db.rawQuery("SELECT contact_number FROM contact_list WHERE grp_code IN (" + TextUtils.join(",", Collections.nCopies(groupCode.size(), "?")) + ")", groupCode.toArray(new String[groupCode.size()]));
Your current code fails to pass the list in the sql-format: = does only support single values, for lists you have to use IN.
Your code would result in a query like this:
SELECT contact_number FROM contact_list WHERE grp_code=["some_id","other_id"]
But what you need (and my code produces) is:
SELECT contact_number FROM contact_list WHERE grp_code IN ('some_id','other_id')
References:
SQL query to find rows with at least one of the specified values
WHERE IN clause in Android sqlite?
IN clause and placeholders
https://developer.android.com/reference/android/text/TextUtils.html#join(java.lang.CharSequence,%20java.lang.Iterable)
You cannot pass an ArrayList to an SQLQuery. To check for multiple values in the same field you have to use the 'in' keyword.
Ex:
SELECT * FROM `table1` where column in ( 'element1', 'element2', 'element3')
In your case,
String str = "";
for(String s: groupCode){
str = str+","+"\'"+s+"\'";
}
//to remove the extra ' in the begining
str = str.substring(1);
return str;
cursor = db.rawQuery("SELECT contact_number FROM contact_list WHERE grp_code IN (?)", new String[]{str});

How to sort HashMap<String,ArrayList> in Android?

How to sort HashMap<String,ArrrayList<String>>?
I am fetching contact name from phone contacts like:
"String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));"
And stored it into one HashMap<String,ArrayList<String>>. But all contacts I am getting in unsorted order.
May I know how to sort those contacts in alphabetical order so that I can display list view in sorted order?
You can retrieve your data from the database already sorted. Just use
String orderBy = ContactsContract.Contacts.DISPLAY_NAME + " DESC";
in your query. After that, usually when you need to use HashMap an have also sorted data, then you use HashMap + ArrayList. In HashMap you keep normal key,value pairs, in ArrayList you keep sorted values (in case if you have already sorted data, you add it to ArrayList while reading, you don't need to sort again).
If you need to sort ArrayList which is value in your HashMap, you can use:
Collections.sort(yourArrayList);
Use below class and pass List of data to the it. It will give you new sorted list.
public class CompareApp implements Comparator<AppDetails> {
private List<AppDetails> apps = new ArrayList<AppDetails>();
Context context;
public CompareApp(String str,List<AppDetails> apps, Context context) {
this.apps = apps;
this.context = context;
Collections.sort(this.apps, this);
}
#Override
public int compare(AppDetails lhs, AppDetails rhs) {
// TODO Auto-generated method stub
return lhs.label.toUpperCase().compareTo(rhs.label.toUpperCase());
}
}
I used following code to get Contacts in Ascending Order of String names as follows:
public void getAllContacts(ContentResolver cr) {
Cursor phones = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,
null, Phone.DISPLAY_NAME + " ASC");
while (phones.moveToNext()) {
String name = phones
.getString(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phoneNumber = phones
.getString(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
name1.add(name);
phno1.add(phoneNumber);
}
phones.close();
}
Code for reference
Hope this is what you need.

Update ListView after delete operation

I am displaying data pulled from the Android OS sqlite database. I am successfully getting the items to delete when I click on them. However I am having an issue refreshing, or updating the listview after the operation.
Below is the code where I delete the contact.
deleteBtn = (Button)v.findViewById(R.id.deleteBtn);
deleteBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "Deleted: " + c.getId() + " " + c.getName(), Toast.LENGTH_SHORT).show();
adapter.deleteContact(c.getId());
updateList();
}
});
Below is the updateList() method:
public void updateList(){
myList.refreshDrawableState();
myList.invalidateViews();
this.notifyDataSetChanged();
}
I included in this method all three ways I tried to refresh but none worked for me. Any idea how I might achieve this?
EDIT: I changed my code thinking this would be the solution but it did not work either:
DbAdapter class delete method():
public boolean deleteContact(int rowId){
getAllContactsList();
return db.delete(DB_TABLE, COLUMN_ID + "=" + rowId, null) > 0;
}
getAllContactsList():
public List<Contact> getAllContactsList(){
List<Contact> contactList = new ArrayList();
Cursor c = db.query(DB_TABLE, new String [] {COLUMN_ID, COLUMN_FNAME, COLUMN_LNAME}, null, null, null, null, null);
//loop through cursor rows and add to list
if(c.moveToFirst()){
do{
Contact contact = new Contact();
contact.setId(Integer.parseInt(c.getString(0)));
contact.setfName(c.getString(1));
contact.setlName(c.getString(2));
contactList.add(contact);
}while(c.moveToNext());
}
return contactList;
}public List<Contact> getAllContactsList(){
List<Contact> contactList = new ArrayList();
Cursor c = db.query(DB_TABLE, new String [] {COLUMN_ID, COLUMN_FNAME, COLUMN_LNAME}, null, null, null, null, null);
//loop through cursor rows and add to list
if(c.moveToFirst()){
do{
Contact contact = new Contact();
contact.setId(Integer.parseInt(c.getString(0)));
contact.setfName(c.getString(1));
contact.setlName(c.getString(2));
contactList.add(contact);
}while(c.moveToNext());
}
return contactList;
}
I thought by getting a new cursor before deleting the contact It would update the list accordingly. Unfortunately It made no difference. Any ideas ?
You have to notify the list's adapter that you have modified the underlying data.
Try using adapter.notifyDataSetChanged();
It seems that you have the Database Adapter class, but you're missing the ArrayAdapter class, which is intended to manage the list of items, displayed in your ListView. Take a look at this example, specifically at the WeatherAdapter.java class.
If I am wrong in my assumptions and the adapter object in your code is not a database adapter, but an ArrayAdapter class, try putting adapter.notifyDataSetChanged() instead of this.notifyDataSetChanged().
Let me know if this helped.
You must requery and and generate new list:
public void updateList(){
clear();
addAll(contactsDbHelper.getAllContactsList()); //addAll works since 11 API version.
notifyDataSetChanged(); //need this is you dissabled auto notify
}
P.S. You should done this job with using Content providers and CursorAdapter, but you need manually notify content provider about changes, because Cursor.requery() is deprecated since 11 version.

Android SQLite rawquery returns only first record

My SQLite query returning only one record, However, the table has multiple rows
cursor=mydb.rawQuery("Select category from items;", null);
I have even tried GROUP BY but still wont work.
I am new to SQLite, would appreciate any help. Thanks.
First of all your string query must not be terminated so instead of passing it as:
"Select category from items;"
you should try passing it as:
"Select category from items"
as mentioned on this page.
Also, are you looping over the cursor? Here is an example of how to get data out of a cursor with a while loop:
ArrayList<String> results = new ArrayList<String>()
while (cursor.moveNext()) {
results.add(cursor.getString(0)); // 0 is the first column
}
First, search:
Cursor cs = myDataBase.rawQuery("Your query", null);
if (cs.moveToFirst()) {
String a = cs.getString(cs.getColumnIndex("your_column_name"));
cs.close();
return a;
}
cs.close();
return "";
Get the information from cursor:
if (cs.moveToFirst()) {
ArrayList<String> results = new ArrayList<String>()
do {
results.add(cs.getString(cs.getColumnIndex("your_column_name")));
} while (cs.moveNext());
}
Without if, I took error in my project. But this worked for me. By the way, your query doesn't look good. If you give some information about your database, we can help much more.
Use this to select all items from the table:
Cursor cursor = db.rawQuery("SELECT * FROM Tablename ,
null);
this can help you
public ArrayList<mydata> getallContents() {
ArrayList<mydata> lst = new ArrayList<mydata>();
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.rawQuery("select * from " + GAMETABLE, null);
if (c.moveToFirst()) {
do {
lst.add(new mydata(c.getString(1),
c.getString(3),c.getString(4),c.getString(5),c.getString(6)));
} while (c.moveToNext());
}
db.close();
return lst;
}
you don't need raw query method. i think that the android way is better in this case:
db.query(items, category, null, null, null, null, null);
than use the cursor how already is written in the other comment.

Categories

Resources