自动补全联系人姓名

1、布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="自动补全联系人姓名"
        android:textColor="@android:color/black"
        android:textSize="30dp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:text="姓名:"
            android:textColor="@android:color/black"
            android:textSize="25dp" />

        <AutoCompleteTextView
            android:id="@+id/edit"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:completionThreshold="1"
            android:textColor="@android:color/black" >

            <requestFocus />
        </AutoCompleteTextView>
    </LinearLayout>

</LinearLayout>

2、创建ContactListAdapter类,继承CursorAdapter类并实现Filterable接口

public class ContactListAdapter extends CursorAdapter implements Filterable {

    private ContentResolver resolver;
    private  String[] columns = new String[] { Contacts._ID, Contacts.DISPLAY_NAME };
    public ContactListAdapter(Context context, Cursor c) {
        super(context, c);//调用父类构造方法
        resolver = context.getContentResolver();//初始化ContentResolver
    }

    @Override
    public void bindView(View arg0, Context arg1, Cursor arg2) {
        ((TextView) arg0).setText(arg2.getString(1));
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        LayoutInflater inflater = LayoutInflater.from(context);
        TextView view = (TextView) inflater.inflate(android.R.layout.simple_dropdown_item_1line, parent, false);
        view.setText(cursor.getString(1));
        return view;
    }

    @Override
    public CharSequence convertToString(Cursor cursor) {
        return cursor.getString(1);
    }

    @Override
    public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
        FilterQueryProvider filter = getFilterQueryProvider();
        if (filter != null) {
            return filter.runQuery(constraint);
        }

        Uri uri = Uri.withAppendedPath(Contacts.CONTENT_FILTER_URI, Uri.encode(constraint.toString()));
        return resolver.query(uri, columns, null, null, null);
    }
}

3、MainActivity

public class MainActivity extends Activity {
	private String[] columns = new String[]{Contacts._ID,Contacts.DISPLAY_NAME};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ContentResolver resolver = getContentResolver();
        Cursor cursor = resolver.query(Contacts.CONTENT_URI, columns, null, null, null);
        ContactListAdapter adapter = new ContactListAdapter(this, cursor);
        AutoCompleteTextView textView = (AutoCompleteTextView)findViewById(R.id.edit);
        textView.setAdapter(adapter);
    }
}

4、AndroidManifest天假读取联系人记录的权限

<uses-permission android:name="android.permission.READ_CONTACTS"/>
自动补全联系人姓名_第1张图片


你可能感兴趣的:(自动补全联系人姓名)