Hi i want to get the mobile number and set it in a edittext
field.
Button onclick()
:
btnContacts.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);
}
});
onActivityResult()
code:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
Cursor cursor = null;
// mName.setText(context.getString(R.string.not_available));
// mNumber.setText(context.getString(R.string.not_available));
if(requestCode == PICK_CONTACT && resultCode == RESULT_OK && data != null){
// Log.d(TAG, "requestCode, resultCode, data ok");
Uri uri = data.getData();
try{
String[] projection = new String[] {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.HAS_PHONE_NUMBER};
// cursor = getContentResolver().query(uri, projection, null, null, null);
cursor = getContentResolver().query(uri, null, null, null, null);
cursor.moveToFirst();
// Log.d(TAG, "Trying to retrieve the name and the number");
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String hasNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.HAS_PHONE_NUMBER));
//Log.d(TAG, "hasNumber "+hasNumber);
//mName.setText(name);
// Log.d(TAG, "contact has telephone number");
//set name and number
String phoneNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
// mNumber.setText(phoneNumber);
Toast toast = Toast.makeText(getApplicationContext(),phoneNumber, Toast.LENGTH_SHORT);
toast.show();
}catch(Exception ex){
Toast toast = Toast.makeText(getApplicationContext(),"fail", Toast.LENGTH_SHORT);
toast.show();
}
if(cursor!= null && !cursor.isClosed()){
cursor.close();
}
}else{
Toast toast = Toast.makeText(getApplicationContext(),"failed", Toast.LENGTH_SHORT);
toast.show();
}
}
When i click the button
the contact book opens, and when i select a contact some error is thrown
Log:
Failed to read row 0, column -1 from a CursorWindow which has 1 rows, 36 columns.
Where am i doing wrong?