我是Android新手,我想在自己的列表视图中显示电话簿中的联系电话。我已经实现了显示名称,但是当我尝试显示数字时,它返回的布尔值为1或0。
我的代码是
public class Test extends ListActivity {
static final String[] CONTACTS_SUMMARY_PROJECTION = new String[] {
Contacts._ID,
Contacts.DISPLAY_NAME,
Contacts.STARRED,
Contacts.TIMES_CONTACTED,
Contacts.CONTACT_PRESENCE,
Contacts.PHOTO_ID,
Contacts.LOOKUP_KEY,
Contacts.HAS_PHONE_NUMBER
};
static final int SUMMARY_ID_COLUMN_INDEX = 0;
static final int SUMMARY_NAME_COLUMN_INDEX = 1;
static final int SUMMARY_STARRED_COLUMN_INDEX = 2;
static final int SUMMARY_TIMES_CONTACTED_COLUMN_INDEX = 3;
static final int SUMMARY_PRESENCE_STATUS_COLUMN_INDEX = 4;
static final int SUMMARY_PHOTO_ID_COLUMN_INDEX = 5;
static final int SUMMARY_LOOKUP_KEY = 6;
static final int SUMMARY_HAS_PHONE_COLUMN_INDEX = 7;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String select = "((" + Contacts.DISPLAY_NAME + " NOTNULL) AND ("
+ Contacts.HAS_PHONE_NUMBER + "=1) AND ("
+ Contacts.DISPLAY_NAME + " != '' ))";
Cursor c =
getContentResolver().query(Contacts.CONTENT_URI, CONTACTS_SUMMARY_PROJECTION, select,
null, Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
startManagingCursor(c);
ContactListItemAdapter adapter = new ContactListItemAdapter(this, R.layout.main, c);
setListAdapter(adapter);
}
private final class ContactListItemAdapter extends ResourceCursorAdapter {
public ContactListItemAdapter(Context context, int layout, Cursor c) {
super(context, layout, c);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
final ContactListItemCache cache = (ContactListItemCache) view.getTag();
cursor.copyStringToBuffer(SUMMARY_NAME_COLUMN_INDEX, cache.nameBuffer);
int size = cache.nameBuffer.sizeCopied;
cursor.copyStringToBuffer(SUMMARY_HAS_PHONE_COLUMN_INDEX, cache.numberView);
int number = cache.numberView.sizeCopied;
cache.nameView.setText(cache.nameBuffer.data, 0, size);
cache.numView.setText(cache.numberView.data, 0, number);
final long contactId = cursor.getLong(SUMMARY_ID_COLUMN_INDEX);
final String lookupKey = cursor.getString(SUMMARY_LOOKUP_KEY);
cache.photoView.assignContactUri(Contacts.getLookupUri(contactId, lookupKey));
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View view = super.newView(context, cursor, parent);
ContactListItemCache cache = new ContactListItemCache();
cache.nameView = (TextView) view.findViewById(R.id.name);
cache.numView = (TextView) view.findViewById(R.id.number);
cache.photoView = (QuickContactBadge) view.findViewById(R.id.badge);
view.setTag(cache);
return view;
}
}
final static class ContactListItemCache {
public TextView nameView;
public TextView numView;
public CharArrayBuffer numberView = new CharArrayBuffer(128);
public QuickContactBadge photoView;
public CharArrayBuffer nameBuffer = new CharArrayBuffer(128);
}
提前致谢。
查看我的更新代码 - >
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ContentResolver cr = new ContentResolver(this.getApplicationContext()) {
};
Cursor phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", null, null);
phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
ContactListItemAdapter adapter = new ContactListItemAdapter(this, R.layout.main, phones);
setListAdapter(adapter);
}
但它显示错误。帮我解决这个问题!
感谢....
答案 0 :(得分:0)
Contacts.HAS_PHONE_NUMBER会返回联系人是否有电话号码的天气。获取实际电话号码
ContactsContract.CommonDataKinds.Phone.NUMBER
使用以下代码获取有关所有联系人的信息。一世。 e使用此URI
Cursor phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", "your contact id", null);
phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
因此您可以在OnCreate()
中查询联系人数据库时使用此代码更新了答案
在OnCreate()中,您使用Phone NUmber和显示名称查询数据库。但现在您需要修改查询,以便获得联系人ID 。
为此,您只需在联系人选择查询中添加ID字段。
String select = "((" + Contacts.DISPLAY_NAME + " NOTNULL) AND ("
+ Contacts.HAS_PHONE_NUMBER + "=1) AND ("
+ Contacts.DISPLAY_NAME + " != '' ) AND (" + Contacts._ID + " NOTNULL ))";
Cursor c =
getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, CONTACTS_SUMMARY_PROJECTION, select,
null, Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
获取此光标后,您需要从此光标获取Contact ID并将该ID传递给下面的查询
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", "Put the contact id here", null);
phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
答案 1 :(得分:0)
这对我来说不合适:
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ number,
number
与CONTACT_ID
的关系如何?