Skip to content

Instantly share code, notes, and snippets.

@cheskos
Last active March 18, 2016 02:42
Show Gist options
  • Save cheskos/58dcf326f03ed132606a to your computer and use it in GitHub Desktop.
Save cheskos/58dcf326f03ed132606a to your computer and use it in GitHub Desktop.
Gets a ContentProvider instance and searches through the ContactsContract in order to obtain the own user's contact
public void setAccountNameOnEditText() {
Cursor c = getActivity().getContentResolver().query(ContactsContract.Profile.CONTENT_URI, null, null, null, null);
int count;
String[] columnNames;
int position;
final ArrayList<String> listNames = new ArrayList<>();
try {
count = c.getCount();
columnNames= c.getColumnNames();
c.moveToFirst();
position = c.getPosition();
if (count == 1 && position == 0) {
for (String columnName : columnNames) {
String columnValue = c.getString(c.getColumnIndex(columnName));
if (columnName.equals("sort_key") || columnName.equals("display_name")
|| columnName.equals("display_name_alt") || columnName.equals("sort_key_alt")) {
listNames.add(columnValue);
}
}
} else {
Toast.makeText(getActivity(), getString(R.string.error_no_name), Toast.LENGTH_SHORT).show();
}
c.close();
} catch (NullPointerException e) {
e.printStackTrace();
Log.e(TAG, "Could not get Cursor from ContentResolver, NPE :(");
throw new NullPointerException("Could not get Cursor from ContentResolver");
}
if(!listNames.isEmpty()) {
etNombreMultado.setText(listNames.get(0));
} else {
Toast.makeText(getActivity(), getString(R.string.error_no_name), Toast.LENGTH_SHORT).show();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment