Ich entwickle eine Android Auto ähnliche App, in der ich das Anrufprotokoll mit Recyclerview und Karten anzeigen möchte.Doppelte Einträge im Anrufprotokoll überspringen
Das funktioniert gut, aber das Anrufprotokoll zeigt alle Protokolle. Sagen wir, wenn ich 3 Anrufe von Peter erhalten habe, möchte ich nicht 3 Einträge sehen, die das zeigen, mit einem Eintrag genug. Das ist so, als würde man so etwas wie "Letzte Kontakte" oder so etwas tun.
Bei Verwendung der Recyclerview und Karten habe ich 3 Klassen erstellt, um die Kontaktinformationen zu speichern: Der benutzerdefinierte Adapter, die Kontaktinformationen und der benutzerdefinierte Ansichtshalter.
Dies ist die Contact Klasse:
public class ContactInfo {
public int id;
public String name;
public String type;
public static final String ID_PREFIX = "ID_";
public static final String NAME_PREFIX = "Name_";
public static final String TYPE_PREFIX = "Type_";
}
im Fragment Dann, wo ich das Anrufprotokoll zeigen, ist es das, was ich tun, um die Protokolle anzuzeigen:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.phone_layout, container, false);
...
ContactAdapter contactAdapter = new ContactAdapter(DisplayCallLog());
...
return view;
}
private ArrayList<ContactInfo> DisplayCallLog() {
ArrayList<ContactInfo> data = new ArrayList<ContactInfo>();
int contactID = 0;
String contactNumber = null;
int logType = 0;
String contactName = null;
String contactType = null;
ContactInfo cI;
int resultLimit = 0;
//Check access to Call Log
if (ActivityCompat.checkSelfPermission(this.getActivity(), Manifest.permission.READ_CALL_LOG) == PackageManager.PERMISSION_GRANTED) {
//Get phone numbers from call log
Cursor cursorCallLog = getActivity().getContentResolver().query(CallLog.Calls.CONTENT_URI,
null, null, null, CallLog.Calls.DATE + " DESC");
while (cursorCallLog.moveToNext() && resultLimit<6) {
contactNumber = cursorCallLog.getString(cursorCallLog.getColumnIndex(CallLog.Calls.NUMBER));
//We also get the call type: Incoming, Outgoing, missed
logType = cursorCallLog.getInt(cursorCallLog.getColumnIndex(CallLog.Calls.TYPE));
resultLimit++;
//With the phone number we search the ID
String number = Uri.encode(contactNumber);
Cursor cursorContactLookup = getActivity().getContentResolver().query(
Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI,
number),
new String[]{ContactsContract.PhoneLookup.DISPLAY_NAME, ContactsContract.PhoneLookup._ID},
null, null, null);
while (cursorContactLookup.moveToNext()) {
contactID = cursorContactLookup
.getInt(cursorContactLookup
.getColumnIndexOrThrow(ContactsContract.PhoneLookup._ID));
//Get the contact name and phone type
Cursor cursorContactDetails = getActivity().getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
new String[] {
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.TYPE,
},
ContactsContract.Data.CONTACT_ID + "=?",
new String[] {String.valueOf(contactID)}, null);
while (cursorContactDetails.moveToNext()) {
contactName = cursorContactDetails
.getString(cursorContactDetails
.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
int type = cursorContactDetails
.getInt(cursorContactDetails
.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.TYPE));
switch (type) {
case ContactsContract.CommonDataKinds.Phone.TYPE_HOME:
contactType = "Home";
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_WORK:
contactType = "Work";
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE:
contactType = "Mobile";
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_OTHER:
contactType = "Other";
break;
}
//Call contactinfo class and save into list
cI = new ContactInfo();
cI.id = contactID;
cI.name = contactName;
cI.type = contactType;
//cI.logType = logType;
//HERE: CHECK IF LIST DOES NOT CONTAIN CURRENT CONTACT
if (!data.contains(cI)) {
data.add(cI);
}
}
cursorContactDetails.close();
}
cursorContactLookup.close();
}
cursorCallLog.close();
}
return data;
}
Das Problem I‘ m ist, dass cI
eine Zeichenfolge wie folgt zeigt:
I/CONTACT_INFO: [email protected]
Wo die letzten Zahlen sind immer anders, auch wenn der gespeicherte Kontakt gleich ist. Es findet also nie den gleichen Kontakt in der Liste, auch wenn es doppelt vorhanden ist.
Also meine Frage ist, wie könnte ich überprüfen, ob der gespeicherte Kontakt bereits in der Liste ist? Ich nehme an, dass das Problem in diesem Fall wegen der Verwendung einer benutzerdefinierten Klasse wie ContactInfo ist.
Haben Sie versucht, den Namen zu vergleichen? – Doppie
@Doppie Ja, ich habe es versucht! – masmic
Vielleicht drucken Sie die Namen von Hand zu vergleichen, sollte dies der richtige Ansatz sein. – Doppie