我创建了一个小程序,用于在列表视图中查看联系人姓名和号码。
public class showcontacts extends Activity
{
ListView lv;
Uri u;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.listcontacts);
lv=(ListView)findViewById(R.id.listView1);
lv.setOnItemClickListener(new mysellisten());
u=Phone.CONTENT_URI;
shownames();
}
public void shownames()
{
String[] p=new String[] {
Contacts._ID,
Contacts.DISPLAY_NAME,
Contacts.HAS_PHONE_NUMBER,
Phone.NUMBER
};
String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '"
+ ("1") + "' and "+ContactsContract.Contacts.HAS_PHONE_NUMBER+"='1'" ;
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME
+ " COLLATE LOCALIZED ASC";
Cursor c=getContentResolver().query(u, p, selection,null, sortOrder);
String from[]=new String[]{ContactsContract.Contacts.DISPLAY_NAME,ContactsContract.CommonDataKinds.Phone.NUMBER};
int id[]={R.id.name,R.id.phoneno};
SimpleCursorAdapter sc=new SimpleCursorAdapter(this,R.layout.phoneview, c, from, id);
lv.setAdapter(sc);
}
class mysellisten implements OnItemClickListener
{
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "called", Toast.LENGTH_SHORT).show();
Uri nu=ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, arg3);
Intent i=new Intent(Intent.ACTION_VIEW,nu);
startActivity(i);
}
}
phoneview是一个带有Imageview和2个textview的简单xml。 我能够获得名称和phonenos.However单击列表视图中的项目显示的记录不正确。它与我单击的记录不匹配。在显示空白屏幕后,有些记录甚至无法打开模拟器返回列表视图。
另外,为什么我在使用Phone.CONTENT_URI时能够使用Contacts表列,但如果使用Contacts.CONTENT_URI,则无法使用字段Phone.NUMBER。 亲切的更新。
答案 0 :(得分:0)
您是否已将联系人permission
添加到AndroidManifest.xml?
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
答案 1 :(得分:0)
您绝对可以使用Contacts.CONTENT_URI
检索联系人姓名和电话号码,请找到执行此操作的代码
try {
Cursor cursor = getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI, null, null, null,
Phone.DISPLAY_NAME + " ASC");
while (cursor.moveToNext()) {
String contactId = cursor.getString(cursor
.getColumnIndex(ContactsContract.Contacts._ID));
String hasPhone = cursor
.getString(cursor
.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
String name = cursor
.getString(cursor
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if ("1".equals(hasPhone) || Boolean.parseBoolean(hasPhone)) {
// You know it has a number so now query it like this
Cursor phones = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = " + contactId, null, null);
while (phones.moveToNext()) {
String dname = cursor
.getString(cursor
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
System.out.println("Name is " + dname);
String phoneNumber = phones
.getString(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
System.out.println("Phone Number is " + phoneNumber);
int itype = phones
.getInt(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
final boolean isMobile = itype == ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE
|| itype == ContactsContract.CommonDataKinds.Phone.TYPE_WORK_MOBILE;
}
phones.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}