Android Api Demos登顶之路(100)View-->AutoComplete:Contacts

/*
 * 这个demo演示了使用AutoCompleteTextView实现根据用户输入的内容自动完成提示对话框的加载。
 * 通过completionThreshold属性设置当用户输入几个字符时显示提示对话框。
 * 在本例中主要演示了对通讯录中存储的联系人的姓名进行检索,自动显示与输入内容相匹配的联系人姓名列表
 */
public class MainActivity extends Activity {
    private static final String[] CONTACT_PROJECTION = new String[] {
            Contacts._ID, Contacts.DISPLAY_NAME };
    private static final int COLUMN_DISPLAY_NAMW = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 获取内容解析者
        ContentResolver resolver = getContentResolver();
        Cursor cursor = resolver.query(Contacts.CONTENT_URI,
                CONTACT_PROJECTION, null, null, null);

        ContactListAdapter adapter=new ContactListAdapter(this, cursor);
        AutoCompleteTextView view=(AutoCompleteTextView) findViewById(R.id.edit);
        view.setAdapter(adapter);
    }

    private class ContactListAdapter extends CursorAdapter  {
        private ContentResolver mContent;

        public ContactListAdapter(Context context, Cursor c) {
            super(context, c);
            mContent = context.getContentResolver();
        }

        @Override
        public void bindView(View view, Context context, Cursor c) {
            ((TextView) view).setText(c.getString(COLUMN_DISPLAY_NAMW));
        }

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

        @Override
        public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
            // 获取一个带有过滤功能的查询器
            FilterQueryProvider filter = getFilterQueryProvider();
            if (filter != null) {
                //System.out.println("过滤查询不为空!");
                filter.runQuery(constraint);
            }

            // 如果获取不到带有过滤功能的查询器则由内容解析者来完成查询
            Uri uri = Uri.withAppendedPath(Contacts.CONTENT_FILTER_URI,
                    Uri.encode(constraint.toString()));

            return mContent.query(uri, CONTACT_PROJECTION, null, null, null);
        }

    }
}

activity.main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="type in the text field for auto-completion" />

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

       <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Name:" />

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

</LinearLayout>

别忘了添加联系人的权限

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值