2012-08-13 49 views
5

我需要顯示一個自動完成的文本框,它將基本上加載聯繫人的電子郵件ID。 我已經使用自定義適配器嘗試過它,但沒有在文本框中填充。根本沒有建議。任何解決方案都將非常有用。帶有聯繫人的自動完成TextView

+2

請發表你的一些代碼,以便我們能夠幫助您更好地 – 2012-08-13 12:31:53

+0

閱讀這篇文章的http:// stackoverflow.com/questions/11879078/search-list-in-our-application-in-android/11879375#11879375這將幫助你 – Akshay 2012-08-13 12:38:47

回答

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); 
} 

注:不要忘記將READ_CONTACTS允許添加到您的Manifest.xml:

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

我在做類似的事情! stackoverflow.com/questions/12854336/autocompletetextview-backed-by-cursorloader – toobsco42 2012-10-12 08:17:16

0

@Korhan肯定是一個非常優雅的方式比我明白了。我的代碼可行,但@ Korhan's更簡單。謝謝。我創建這個自定義適配器類讀取聯繫人

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); 
     } 

    } 

和主要活動:

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()); 
相關問題