我正在开发类似Android Auto的应用,我希望使用recyclerview和卡片显示通话记录。
这很好用,但是通话记录显示所有日志。让我们说,如果我收到彼得的3个电话,我不想看到有3个条目显示这个,只有一个条目。这就像做"最近的联系人"或类似的东西。
使用recyclerview和卡时,我已创建了3个类来保存联系人信息:自定义适配器,联系信息和自定义视图持有者。
这是ContactInfo类:
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_";
}
然后,在我显示调用日志的片段中,这就是我显示日志的方法:
@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;
}
我遇到的问题是cI
显示的字符串如下:
I/CONTACT_INFO: com.example_infodash.phone.ContactInfo@41c9e198
即使保存的联系人是相同的,最后的数字总是不同的。所以它永远不会在列表中找到相同的联系人,即使是重复的。
所以我的问题是,如何检查保存的联系人是否已经在列表中?我想在这种情况下的麻烦是因为使用了像ContactInfo这样的自定义类。
答案 0 :(得分:1)
我遇到的问题是cI显示如下字符串:
I / CONTACT_INFO:com.example_infodash.phone.ContactInfo@41c9e198
此问题的解决方案:覆盖patients = Demographic.objects.filter(author__institution__department=department)
toString
方法
ContactInfo
对于ArrayList“包含”问题,您必须覆盖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_";
@Override
public String toString() {
return "{name: " + name + ", type: " + type + "}";
}
}
中的equals()
。像这样:
ContactInfo
如果您在Android Studio中,则可以自动创建它。转到ContactInfo类,右键单击它,选择:
public class ContactModel {
public String name;
public String phone;
public ContactModel(String name, String phone) {
this.name = name;
this.phone = phone;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ContactModel that = (ContactModel) o;
if (name != null ? !name.equals(that.name) : that.name != null) return false;
return phone != null ? phone.equals(that.phone) : that.phone == null;
}
}
你的equals()方法:
Generate... -> equals() and hashCode()