我使用make语法显示hp中的列表联系人,但仍显示重复的联系人,有人可以帮助我实现此功能吗?
这是我的代码
功能加载联系人
在这里我很困惑如何在不需要将重复联系人添加到列表视图的情况下进行过滤
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);
这是用于文件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;
}
}
联系人模型的此文件类
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;
}
}
答案 0 :(得分:0)
在您的活动中(第一段代码):
Set<ContactModel> contactModelSet = new HashSet<>();
// the same ...
ArrayList<ContactModel> contactModelArrayList = new ArrayList<>(contactModelSet);
contactAllAdapter = new ContactAllAdapter(this,contactModelArrayList);
在ContactModel类中:
@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;
}