-1

I'm writing a program to display my contact list in hp, but I always get duplicate contacts. Where is my code not correct?

display my code run

This is my code

Here I'm confused how to make a filter where duplicate contacts do not need to be added to the listview:

contactModelArrayList = new ArrayList<>();
String[] projection = new String[]{
        ContactsContract.CommonDataKinds.Phone.NUMBER,
        ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME
};
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
        projection,
        ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '1'",
        null,
        ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME+" COLLATE LOCALIZED 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));

    ContactModel contactModel = new ContactModel();
    contactModel.setName(name);
    contactModel.setNumber(phoneNumber);

    contactModelArrayList.add(contactModel);

}

phones.close();

contactAllAdapter = new ContactAllAdapter(this,contactModelArrayList);
listView.setAdapter(contactAllAdapter);

And this for file ContactAll Adapter

public class ContactAllAdapter extends BaseAdapter {
    private Context context;
    private ArrayList<ContactModel> contactModels;
    private String kode, nomor;

    public ContactAllAdapter(Context context, ArrayList<ContactModel> contactModels) {
        this.context = context;
        this.contactModels = contactModels;
    }

    @Override
    public int getViewTypeCount() {
        if(getCount() > 0){
            return getCount();
        }else{
            return super.getViewTypeCount();
        }
    }

    @Override
    public int getCount() {
        return contactModels.size();
    }

    @Override
    public Object getItem(int position) {
        return contactModels.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;

        if (convertView == null) {
            holder = new ViewHolder();
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.row_all_contact, null, true);

            holder.cname = convertView.findViewById(R.id.contact_name);
            holder.cnumber = convertView.findViewById(R.id.contact_number);
            holder.cinvite = convertView.findViewById(R.id.contact_invite);

            convertView.setTag(holder);
        }else {
            // the getTag returns the viewHolder object set as a tag to the view
            holder = (ViewHolder)convertView.getTag();
        }

        holder.cname.setText(contactModels.get(position).getName());
        holder.cnumber.setText(contactModels.get(position).getNumber());

        holder.cinvite.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                final String appPackageName = context.getPackageName();
                String text = context.getResources().getString(R.string.share_app_text);
                String link = "https://play.google.com/store/apps/details?id=" + appPackageName;//app link is auto generated by using package name

                Intent sendIntent = new Intent(Intent.ACTION_VIEW);
                sendIntent.putExtra("sms_body", text+"\n\n"+link);
                sendIntent.putExtra("address", contactModels.get(position).getNumber());
                sendIntent.setType("vnd.android-dir/mms-sms");
                context.startActivity(sendIntent);
            }
        });

        return convertView;
    }

    private class ViewHolder {

        protected TextView cname, cnumber;
        protected Button cinvite;
    }
}

And this file class of Contact Model

public class ContactModel {

    private String name, number;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Arif MH
  • 21
  • 8
  • here is my screenshot for that https://drive.google.com/file/d/1EAxPGXJSCtT8UywYhkg97ygFXTWxIGN2/view – Arif MH Feb 01 '19 at 06:49
  • Use Set, not List for the contacts. Then convert set to list `List l = new ArrayList(set)`, the set will avoid duplication. But beware - ContactModel must implement compare and equals. – Evgeni Enchev Feb 01 '19 at 07:15
  • can you give me example for that? – Arif MH Feb 01 '19 at 07:26

1 Answers1

1

In your activity (first piece of code):

    Set<ContactModel> contactModelSet = new HashSet<>();

    // the same ...

    ArrayList<ContactModel> contactModelArrayList = new ArrayList<>(contactModelSet);

    contactAllAdapter = new ContactAllAdapter(this,contactModelArrayList);

In ContactModel class:

@Override
public int hashCode() {
    // calculate hash code if needed, something like this
    return this.name.hashCode() + this.number.hashCode();
}

@Override
public boolean equals(Object obj) {
    if (obj instanceof ContactModel) {
        ContactModel cm = (ContactModel) obj;
        return this.getName().equals(cm.getName()) && this.getNumber().equals(cm.getNumber());
    }
    return false;
}

@Override
public int compareTo(ContactModel o) {
    // implement if you need to order
    // don't know your criteria
    // must return -1, 0, 1 if this is less, equal or greater than o
    return 0;
}
Evgeni Enchev
  • 552
  • 3
  • 10
  • it's same, doesn't work... it is still display duplicate contact – Arif MH Feb 01 '19 at 08:06
  • Can you debug or log the equal contacts? Java set doesn't admit equals objects, so ther must be something in your ContactModel. Just print out the objects before adding to the set. And implement a nice toString method in ContactModel to see name and number – Evgeni Enchev Feb 01 '19 at 08:14
  • does the problem not come when taking contact data? because my query is like taking all contact data on my cellphone, now my cell phone has a google account, wa, etc which seems to sync with contacts ... so what's the problem not from there? – Arif MH Feb 01 '19 at 08:31
  • 1
    Well, it could be, but if you have duplicated contacts and put two equal objects in a set, the set will accept only one. That for we implement the equals method. – Evgeni Enchev Feb 01 '19 at 08:34
  • the number get value for example 0857-9119-8270 and 085791198270... well, how to change that number to same number? and any value for example +62 896-7975-9830 and +6289679759830... can you help me for this case? i'm stuck these several days :') – Arif MH Feb 01 '19 at 09:25
  • The easiest way would be to remove all spaces and "-". Not very elegant but... The other way could be to apply a pattern like "+XX XXX-XXXX-XXXX" but this will be much more costful. Just use `phoneNumber = phoneNumber.replace("-", "").replace(" ", "");` – Evgeni Enchev Feb 01 '19 at 09:37
  • then work to make them same, but the contact still display duplicate. pure duplicate – Arif MH Feb 01 '19 at 10:00
  • here screenshot result of them https://drive.google.com/file/d/16AKhbwR9D0gNh2pQv9X0wUU95_PDdpEh/view?usp=sharing – Arif MH Feb 01 '19 at 10:03
  • Do the replace before adding to ContactModel or in set methods. And please print and show the values. – Evgeni Enchev Feb 01 '19 at 10:06
  • https://drive.google.com/file/d/1eUxF3p5y6aivgk-6bUhB12IggMM_q_NE/view?usp=sharing here for result of them – Arif MH Feb 01 '19 at 10:16
  • I guess you didn't overwrite hashCode. Use this: `return 31 * this.name.hashCode() + this.number.hashCode();`. May be you need some null checks and same for `equals` but this way will work for not null values. – Evgeni Enchev Feb 01 '19 at 11:00