我想在EditText
框中键入文本时显示存储在Android手机中的联系人。我想在消息屏幕上显示EditText
框下方的联系人。我可以使用ContactsContract
检索联系人,但我不知道如何在Android中显示消息传递屏幕。有人知道这个吗?
我目前的代码:
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(
cur.getColumnIndex(ContactsContract.Contacts._ID));
System.out.println("contactsID-->>>"+id);
String name = cur.getString(
cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
System.out.println("contactsName-->>>"+name);
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Cursor pCur = cr.query(Contacts.Phones.CONTENT_URI, null,
Contacts.Phones.PERSON_ID +" = ?",
new String[]{id}, null);
int i=0;
int pCount = pCur.getCount();
while (pCur.moveToNext()) {
String phoneNum = pCur.getString(
pCur.getColumnIndex(Contacts.Phones.NUMBER));
System.out.println("PhoneNum-->>>"+phoneNum);
}
// Query phone here. Covered next
}
}
}
答案 0 :(得分:1)
您必须使用AutoCompleteTextView
并将其填充到您的联系人中。 DOC
答案 1 :(得分:0)
您可以使用自己的类来存储联系信息,根据您的要求自定义此类: -
public class History {
private static final int VERSION = 1;
private static final String FILENAME = "history.dat";
private Context ctx;
private ArrayList<String> list;
public History( Context ctx ) {
this.ctx = ctx;
}
public ArrayAdapter<String> getArrayAdapter() {
return new ArrayAdapter<String>( ctx, android.R.layout.simple_dropdown_item_1line, list );
}
public void addUrl( String url ) {
if (!list.contains( url )) list.add( url );
}
public int size() {
return list.size();
}
public void read() {
list = new ArrayList<String>();
try {
DataInputStream dis = new DataInputStream( ctx.openFileInput( FILENAME ));
dis.readInt(); // VERSION
int n = dis.readInt();
while (n-- > 0) list.add( dis.readUTF());
}
catch (IOException e) {
}
}
public void write() {
try {
DataOutputStream dos = new DataOutputStream( ctx.openFileOutput( FILENAME, 0 ));
dos.writeInt( VERSION );
dos.writeInt( list.size());
for (String url : list) dos.writeUTF( url );
}
catch (IOException e) {
}
}
}
在你的主要课程中:
History historyObject= new History(MainActivity.this)
while (cursor.moveNext()) {
historyObject.addUrl(cursor.getString(0));
}
使用自动完成文本框而不是编辑文本
autocompleteText.setAdapter(historyObject.getArrayAdapter());
希望它会对你有所帮助:)。
答案 2 :(得分:0)
您需要使用AutoCompleteTextView并使用从ContactsContract中读取的联系人姓名来填充适配器。代码:
String CONTACTS[] = null;
Cursor contactsCursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, new String[]{Contacts.DISPLAY_NAME}, null, null, null);
if(contactsCursor!=null){
CONTACTS = new String[contactsCursor.getCount()];//(contactsCursor.getCount())];
int i=0;
while(contactsCursor.moveToNext()) {
CONTACTS[i] = contactsCursor.getString(contactsCursor.getColumnIndex(Contacts.DISPLAY_NAME));
i++;
}
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, CONTACTS);
AutoCompleteTextView textView = (AutoCompleteTextView)
findViewById(R.id.autocompleteTextView);
textView.setAdapter(adapter);