我试图从显示名称和电话号码获取本地数据库中的联系人列表,并在复选框中显示每个联系人,但它不起作用我显示复选框中的姓名和电话号码
这是我一直在努力
LinearLayout ll;
CheckBox ch1;
String name;
String number;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main1);
Button save = (Button) findViewById(R.id.bSave);
Button retur = (Button) findViewById(R.id.bReturn);
retur.setOnClickListener(this);
ch1= (CheckBox)findViewById(R.id.checkBox1);
ContentResolver cr = getContentResolver();
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI,null,null, null, null);
Cursor phones = cr.query(Phone.CONTENT_URI, null,null,null,null);
String number= phones.getString(phones.getColumnIndex(Phone.NUMBER));
String contactId =cursor.getString(cursor.
getColumnIndex(ContactsContract.Contacts._ID));
ch1.setText(contactId+" : "+number);
}
帮助
是的,我测试了它,但总是有错误...我认为错误来自这里:ContentResolver cr = getContentResolver();游标cursor = cr.query(ContactsContract.Contacts.CONTENT_URI,null,null,null,null); Cursor phones = cr.query(Phone.CONTENT_URI,null,null,null,null); String number = phones.getString(phones.getColumnIndex(Phone.NUMBER)); String contactId = cursor.getString(cursor。 getColumnIndex(ContactsContract.Contacts._ID)); chl.setText(contactId +“:”+ number); ch1.setText(contactId +“:”+ number); } – 2012-02-19 20:50:55
以及多数民众赞成在相当多的代码......什么也行的logcat的说,它的呢?什么错误? – bwoogie 2012-02-19 20:54:01
也确保您在完成操作后关闭光标。 – bwoogie 2012-02-19 20:54:30