如何将联系人光标数据绑定到带有芯片的MultiAutoCompleteTextView



我已经做了将近两个星期了。我相信我已经接近了,但我不能让最终产品工作。在我的应用程序中有一个类似于电子邮件的功能,但它使用的不是电子邮件地址,而是电话号码。android上的gmail应用程序能够使用带有芯片的MultiAutoCompleteTextView 来显示收件人的电子邮件地址。这就是我想做的。通过我的研究和试验,我能够创建MultiAutoCompleteTextView,但没有芯片。这里有许多关于同一主题的问题和答案。但无一例外,我发现没有一个例子绑定数据从用户的联系人簿到MultiAutoCompleteTextView。我看到的所有示例都使用了它们自己组成的简单数组,就好像最后一步是微不足道的一样。但最后一步是我卡住的地方。每次我尝试将联系人本绑定到带有芯片的MultiAutoCompleteTextView时,代码都会失败。所以我的问题非常具体:我如何将联系人本绑定到带有芯片的MultiAutoCompleteTextView ?对于某些上下文,我已经阅读了Android应用程序中的芯片小部件以及其他一些文章和响应。有人知道我的具体用例吗?gmail用例?

编辑

下面是我使用https://github.com/splitwise/TokenAutoComplete的代码。但是我的适配器的getView从来没有被调用过。

代码绑定适配器到editText

mAdapter = new ContactAdapter(getActivity(), mPeopleList, R.layout.contacts_row);
multiEdit = (ContactsAutoCompleteView) view.findViewById(R.id.multi_edit);
multiEdit.setAdapter(mAdapter);

加载通讯录的代码

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);
}
我的适配器

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 show

public class Contact implements Serializable {
    private static final long serialVersionUID = 1L;
    private String name;
    private String phone;
    private String type;
}

我的TokenCompleteTextView: ??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);
}

}

几个星期前,我遇到了你的情况,我所做的是使用一个芯片库

我用了这个

关于与联系人的绑定,请更具体地说明您被卡在哪里以及您得到了什么?

最新更新