我想从我的Android手机上读取联系电话。我使用内容提供程序,但我得到空值。这是我的代码:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txt = (TextView)findViewById(R.id.txt);
String column[] = new String[]{People.NAME,People.NUMBER};
Cursor cur = this.managedQuery(People.CONTENT_URI, column, null, null, null);
if(cur.moveToFirst())
{
String name;
String number;
do
{
number = cur.getString(cur.getColumnIndex((People.NUMBER)));
str+="\n"+number;
}
while(cur.moveToNext());
txt.setText(str);
}
}
提前致谢。
答案 0 :(得分:0)
我现在无法检查,但我要改变的一件事是你不需要使用
cur.getColumnIndex((People.NUMBER))
因为您已经知道该列是投影中的第二列(所以index = 1)
答案 1 :(得分:0)
试试这个:
private Cursor getContactCursor(Context context) {
String[] projection = { ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER };
return context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, projection, null, null, null);
}
并在onCreate()..
contactsCursor = getContactsCursor(this);
while (contactsCursor.moveToNext()) {
String name = contactsCursor.getString(0);
String number = contactsCursor.getString(1);
}