[英]How do I bind contact cursor data to a MultiAutoCompleteTextView with chips
I have been at this for almost two weeks now. 我到这已经快两个星期了。 I believe I have come close, but I can't quite get the final product to work. 我相信我已经接近了,但是我无法完全使最终产品正常工作。 I have a functionality similar to email in my app. 我的应用程序具有类似于电子邮件的功能。 But instead of email addresses, it uses phone numbers. 但是,它使用电话号码代替电子邮件地址。 The gmail app on android is able to use a MultiAutoCompleteTextView with chips to display recipient email addresses. Android上的gmail应用程序可以使用带有芯片的MultiAutoCompleteTextView来显示收件人的电子邮件地址。 That is what I want to do. 那就是我想做的。 Through my research and trials, I am able to create the MultiAutoCompleteTextView, but without the chips. 通过我的研究和试验,我能够创建MultiAutoCompleteTextView,但是没有芯片。 There are a number of questions and answers here on the same subject. 在同一主题上有许多问题和答案。 But invariably, none of the ones I find has an example that binds the data from a user's contact book to the MultiAutoCompleteTextView. 但总是可以找到一个示例,该示例没有将用户通讯录中的数据绑定到MultiAutoCompleteTextView的示例。 All the examples I see use their own made up simple arrays, as if the final step were trivial. 我看到的所有示例都使用它们自己组成的简单数组,好像最后一步是微不足道的。 But the final step is where I am stuck. 但是最后一步是我陷入困境。 Each time I try to bind the contact book to an MultiAutoCompleteTextView with chips, the code fails. 每次我尝试将通讯录绑定到带有芯片的MultiAutoCompleteTextView时,代码都会失败。 So my question is quite extremely specific: How do I bind the contact book to an MultiAutoCompleteTextView with chips? 所以我的问题非常具体: 如何将通讯录绑定到带有芯片的MultiAutoCompleteTextView? For some context, I have already read Chips widget in Android application and bunch of other articles and responses besides. 在某些情况下,我已经阅读了Android应用程序中的Chips小部件 ,此外还阅读了其他文章和响应。 Is anyone aware of my specific use case? 有人知道我的特定用例吗? The gmail use case? Gmail用例?
EDIT 编辑
Following is the code I use with https://github.com/splitwise/TokenAutoComplete . 以下是我在https://github.com/splitwise/TokenAutoComplete中使用的代码。 But the getView
of my adapter is never called. 但是我的适配器的getView
从未被调用。
code to bind adapter to editText 将适配器绑定到editText的代码
mAdapter = new ContactAdapter(getActivity(), mPeopleList, R.layout.contacts_row);
multiEdit = (ContactsAutoCompleteView) view.findViewById(R.id.multi_edit);
multiEdit.setAdapter(mAdapter);
code to load contact book 加载通讯录的代码
public void PopulatePeopleList() {
mPeopleList.clear();
Cursor people = getActivity().getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null,
null);
while (people.moveToNext()) {
String contactName = people.getString(people.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String contactId = people.getString(people.getColumnIndex(ContactsContract.Contacts._ID));
String hasPhone = people.getString(people.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if (Integer.parseInt(hasPhone) > 0) {
// You know have the number so now query it like this
Cursor phones = getActivity().getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null);
while (phones.moveToNext()) {
// store numbers and display a dialog letting the user select which.
String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
String numberType = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
if (numberType.equals("0")) {
numberType = "Work";
} else if (numberType.equals("1")) {
numberType = "Home";
} else if (numberType.equals("2")) {
numberType = "Mobile";
} else {
numberType = "Other";
}
mPeopleList.add(new Contact(contactName, phoneNumber, numberType));
}
phones.close();
}
}
people.close();
getActivity().startManagingCursor(people);
}
my adapter 我的适配器
public class ContactAdapter extends ArrayAdapter<Contact> {
private final Context mContext;
private final int mRowResourceId;
private final LayoutInflater mInflater;
ArrayList<Contact> mContacts;
public ContactAdapter(Context context, ArrayList<Contact> contacts, int contactsRow) {
super(context, contactsRow, contacts);
mContext = context;
mRowResourceId = contactsRow;
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mContacts = contacts;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
if (null == rowView) {
rowView = mInflater.inflate(mRowResourceId, parent, false);
ViewHolder viewsHolder = new ViewHolder();
viewsHolder.name = (TextView) rowView.findViewById(R.id.contact_name);
viewsHolder.phone = (TextView) rowView.findViewById(R.id.contact_phone);
viewsHolder.type = (TextView) rowView.findViewById(R.id.contact_type);
rowView.setTag(viewsHolder);
}
ViewHolder viewsHolder = (ViewHolder) rowView.getTag();
Contact con = mContacts.get(position);// getItem(position);
viewsHolder.name.setText(con.getName());
viewsHolder.phone.setText(con.getPhone());
viewsHolder.type.setText(con.getType());
return rowView;
}
static class ViewHolder {
TextView name;
TextView phone;
TextView type;
}
}
my contact object: accessors and constructor not shown 我的联系对象:访问器和构造函数未显示
public class Contact implements Serializable {
private static final long serialVersionUID = 1L;
private String name;
private String phone;
private String type;
}
My TokenCompleteTextView: ?? 我的TokenCompleteTextView:?? what goes inside defaultObject defaultObject里面有什么
public class ContactsAutoCompleteView extends TokenCompleteTextView {
public ContactsAutoCompleteView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected View getViewForObject(Object object) {
Contact p = (Contact) object;
LayoutInflater l = (LayoutInflater) getContext().getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
RelativeLayout view = (RelativeLayout) l.inflate(R.layout.contacts_row,
(ViewGroup) ContactsAutoCompleteView.this.getParent(), false);
((TextView) view.findViewById(R.id.contact_name)).setText(p.getName());
((TextView) view.findViewById(R.id.contact_phone)).setText(p.getPhone());
((TextView) view.findViewById(R.id.contact_type)).setText(p.getType());
return view;
}
@Override
protected Object defaultObject(String completionText) {
//what goes here?
}
@Override
public void setSelected(boolean selected) {
super.setSelected(selected);
}
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.