显示联系人号码在Android 2.1的我自己的ListView [英] Show contact number in my own listview in android 2.1
本文介绍了显示联系人号码在Android 2.1的我自己的ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是新来的Android和我想在我自己的列表视图显示从电话簿中的联系人号码。我已经实现显示的名字,但是当我试图显示数字,则返回布尔数为1或0。
I am new to android and I want to display a contact number from phone book in my own listview. I had achieved to display the name but when I am trying to display the number then it returns the Boolean number that is 1 or 0.
我的code是
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);
}
在此先感谢。
Thanks in advance.
请参阅我的更新code - >
See my updated code ->
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);
}
但它显示错误。帮我解决这个问题!
But it shows error. Help me to solve this!
感谢....
推荐答案
这看起来不正确对我说:
This does not look right to me:
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ number,
如何号
涉及到 CONTACT_ID
?
这篇关于显示联系人号码在Android 2.1的我自己的ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文