MultiAutoCompleteTextView with contacts phone numbers

放肆的年华 提交于 2019-11-28 02:17:34

try this:

final Resources res = getResources();
LinearLayout ll = new LinearLayout(this);
AutoCompleteTextView tv = new AutoCompleteTextView(this);
tv.setThreshold(1);
String[] from = { Phone.DISPLAY_NAME };
int[] to = { android.R.id.text1 };
SimpleCursorAdapter a = new SimpleCursorAdapter(this, android.R.layout.simple_dropdown_item_1line, null, from, to, 0);
a.setStringConversionColumn(2); // Phone.NUMBER
ViewBinder viewBinder = new ViewBinder() {
    @Override
    public boolean setViewValue(View v, Cursor c, int index) {
        TextView tv = (TextView) v;
        int typeInt = c.getInt(3); // Phone.TYPE
        CharSequence type = Phone.getTypeLabel(res, typeInt, null);
        // Phone.DISPLAY_NAME + Phone.NUMBER + type
        tv.setSingleLine(false);
        tv.setText(c.getString(1) + "\n" + c.getString(2) + " " + type);
        return true;
    }
};
a.setViewBinder(viewBinder);
FilterQueryProvider provider = new FilterQueryProvider() {
    @Override
    public Cursor runQuery(CharSequence constraint) {
        // run in the background thread
        Log.d(TAG, "runQuery constraint: " + constraint);
        if (constraint == null) {
            return null;
        }
        ContentResolver cr = getContentResolver();
        Uri uri = Uri.withAppendedPath(Phone.CONTENT_FILTER_URI, constraint.toString());
        String[] proj = { BaseColumns._ID, Phone.DISPLAY_NAME, Phone.NUMBER, Phone.TYPE, };
        return cr.query(uri, proj, null, null, null);
    }
};
a.setFilterQueryProvider(provider);
tv.setAdapter(a);
ll.addView(tv, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
setContentView(ll);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!