你可以用這一個嘗試:
你如何修改要顯示的UI
//我只是硬編碼數retrive數和追加。
public class MainActivity extends Activity implements TextWatcher {
String[] arrcontact = null;
AutoCompleteTextView myAutoComplete;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myAutoComplete = (AutoCompleteTextView) findViewById(R.id.multiAutoCompleteTextView1);
loadContact();
myAutoComplete.addTextChangedListener(this);
myAutoComplete.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, arrcontact));
}
private void loadContact() {
Cursor cursor = getContacts();
arrcontact = new String[30];
int count = 0;
while (cursor.moveToNext()) {
String displayName = cursor.getString(cursor
.getColumnIndex(ContactsContract.Data.DISPLAY_NAME));
arrcontact[count] = displayName + "\n" + "908228282";
count++;
if (count == 30)
break;
}
}
private Cursor getContacts() {
// Run query
final ContentResolver cr = getContentResolver();
String[] projection = { ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts._ID };
String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = ?";
String[] selectionArgs = { "1" };
final Cursor contacts = cr.query(ContactsContract.Contacts.CONTENT_URI,
projection, selection, selectionArgs, "UPPER("
+ ContactsContract.Contacts.DISPLAY_NAME + ") ASC");
return contacts;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
}
我發現我的解決方案在這裏:http://stackoverflow.com/questions/12400504/selecting-contact-from-autocomplete-textview?rq=1 – Regnarg