自动完成文本框与联系人

4

我需要展示一个自动完成文本框,它会加载联系人的电子邮件地址。 我已经尝试使用自定义适配器,但文本框中没有任何内容被填充。没有任何建议。任何解决方案都将非常有用。


2
请发布你的一些代码,这样我们才能更好地帮助你。 - Korhan Ozturk
阅读此帖子 http://stackoverflow.com/questions/11879078/search-list-in-our-application-in-android/11879375#11879375 这将有助于您。 - Akshay
2个回答

9

请尝试以下方法:

ArrayList<String> emailAddressCollection = new ArrayList<String>();

ContentResolver cr = getContentResolver();

Cursor emailCur = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, null, null, null);

while (emailCur.moveToNext())
{
    String email = emailCur.getString(emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
            emailAddressCollection.add(email);
}
emailCur.close();

String[] emailAddresses = new String[emailAddressCollection.size()];
emailAddressCollection.toArray(emailAddresses);

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
             android.R.layout.simple_dropdown_item_1line, emailAddresses);
AutoCompleteTextView textView = (AutoCompleteTextView)findViewById(R.id.YOUR_TEXT_VIEW);
     textView.setAdapter(adapter);
 }

注意: 不要忘记在Manifest.xml中添加READ_CONTACTS权限:

<uses-permission android:name="android.permission.READ_CONTACTS" />

我正在这里做类似的事情!stackoverflow.com/questions/12854336/autocompletetextview-backed-by-cursorloader - Etienne Lawlor

0

@Korhan 肯定是比我想出来的更优雅的方式。我的代码虽然能用,但 @Korhan 的更简单。谢谢。我创建了这个自定义适配器类来读取联系人

class ContactListAdapter extends CursorAdapter implements Filterable {  
        private ContentResolver mCR;

        public ContactListAdapter(Context context, Cursor c,boolean a) {  
            super(context, c, true);  
            mCR = context.getContentResolver();  
        }


        @Override
        public void bindView(View view, Context context, Cursor cursor) {


                 ((TextView) view).setText(cursor.getString(1));
        }


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


                view.setText(cursor.getString(1));

                return view;

        }  
        @Override
        public String convertToString(Cursor cursor) {  



            return cursor.getString(1);
        }
        public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
            if (getFilterQueryProvider() != null) {
                return getFilterQueryProvider().runQuery(constraint);
        }

        StringBuilder buffer = null;
        String[] args = null;
        if (constraint != null) {
            buffer = new StringBuilder();
            buffer.append("UPPER(");
            buffer.append(ContactsContract.CommonDataKinds.Email.ADDRESS);
            buffer.append(") GLOB ?");
            args = new String[] { constraint.toString().toUpperCase() + "*" };
        }

        return mCR.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,CreateEventActivity.PEOPLE_PROJECTION ,buffer == null ? null : buffer.toString(), args,
               null);
        }

    }

还有主要的Activity:

MultiAutoCompleteTextView act = (MultiAutoCompleteTextView)findViewById(R.id.attende_list);
ContentResolver content = getContentResolver();
Cursor cursor = content.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,PEOPLE_PROJECTION, null, null, null);
ContactListAdapter adapter = new ContactListAdapter(this, cursor, true);
act.setThreshold(0);
act.setAdapter(adapter);
act.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接