currently i have a application which will retrieve all the contact
details and will display all the available Contact Names.But now i want to retrieve Home,work numbers.I search for this everywhere but i couldn't. how can
i achieve this? please help me.
Thanks.
I used this code to get Home Numbers.
Cursor c = getContentResolver().query(Data.CONTENT_URI,
new String[] {Data._ID, Phone.NUMBER, Data.MIMETYPE},
Data.RAW_CONTACT_ID + "=?" + " AND "
+ Data.MIMETYPE + "='" + Phone.TYPE_HOME + "'",
new String[] {String.valueOf(contactId)}, null);
But what i need is to get Home,Work,Mobile (with name,email address,etc) numbers using single query.
using this code it always returns me type=0
Cursor c = getContentResolver().query(Data.CONTENT_URI,
null,
Data.CONTACT_ID + "=?",
new String[] {String.valueOf(contactId)}, null);
while(c.moveToNext()){
int type = c.getInt(c.getColumnIndex(Phone.TYPE));
..
}
try this:
int type = mCursor.getInt(mCursor.getColumnIndex(Phone.TYPE));
the "type" value will be 1,2,3 or 4 where
TYPE_HOME = 1;
TYPE_MOBILE = 2;
TYPE_WORK = 3;
TYPE_OTHER = 7;
Ok finally i completed the task.here my code
String mobile = "";
String home="";
String work="";
String fax="";
String other="";
String disName="";
String pName="";
String fName="";
String lName="";
String sName="";
String mName="";
String postBox="";
String streat="";
String country="";
String emailAdd="";
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null,null, null, null);
while (cur.moveToNext()) {
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
disName = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
///*
Cursor phones = cr.query(Phone.CONTENT_URI, null,
Phone.CONTACT_ID + " = " + id, null, null);
while (phones.moveToNext()) {
String number = phones.getString(phones.getColumnIndex(Phone.NUMBER));
int type = phones.getInt(phones.getColumnIndex(Phone.TYPE));
System.out.println("Numeber= "+number);
switch (type) {
case Phone.TYPE_HOME:
home=number;
break;
case Phone.TYPE_MOBILE:
mobile=number;
break;
case Phone.TYPE_WORK:
work=number;
break;
case Phone.TYPE_OTHER:
other=number;
break;
case Phone.TYPE_FAX_HOME:
fax=number;
break;
case Phone.TYPE_FAX_WORK:
fax=number;
break;
}
}
phones.close();
Cursor name = cr.query(Data.CONTENT_URI, null,
Data.CONTACT_ID + " ="+id +" AND "+Data.MIMETYPE+"='"+StructuredName.CONTENT_ITEM_TYPE+"'", null, null);
while(name.moveToNext()){
sName=name.getString(name.getColumnIndex(StructuredName.PREFIX));
fName=name.getString(name.getColumnIndex(StructuredName.GIVEN_NAME));
mName=name.getString(name.getColumnIndex(StructuredName.MIDDLE_NAME));
lName=name.getString(name.getColumnIndex(StructuredName.FAMILY_NAME));
sName=name.getString(name.getColumnIndex(StructuredName.SUFFIX));
System.out.println(mName);
}
Cursor address = cr.query(Data.CONTENT_URI, null,
Data.CONTACT_ID + " ="+id +" AND "+Data.MIMETYPE+"='"+StructuredPostal.CONTENT_ITEM_TYPE+"'", null, null);
while(address.moveToNext()){
postBox=address.getString(address.getColumnIndex(StructuredPostal.POBOX));
streat=address.getString(address.getColumnIndex(StructuredPostal.STREET));
mName=address.getString(address.getColumnIndex(StructuredPostal.COUNTRY));
System.out.println(postBox);
}
Cursor email = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
Data.CONTACT_ID + " ="+id , null, null);
while(email.moveToNext()){
emailAdd=email.getString(email.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
}
In where clause of your query use Phone.TYPE_HOME.. this will give you the desired nor..
Related
I was writing a code to get the contacts in the phone with the below code
StoreContacts.add(name + "\n"+ phonenumber)
How do I get only the name from the list.
private void showContacts() {
c = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, ContactsContract.Contacts.DISPLAY_NAME + " ASC ");
contacts = new ArrayList();
while (c.moveToNext()) {
String contactName = c.getString(c.getColumnIndex( ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME ));
String phNumber = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
contacts.add(contactName + "\n" + phNumber);
}
c.close();
}
You can use String.split() method to split string by given strings
for(int i=0;i<contacts.size();i++){
String strings[] = contacts.get(i).toString().split("\\n");
String name= strings[0];
Log.i("name",name);
}
I'm trying to get a single row from database.But it is not generated query. The questions marks still seems. What am i missing ?
public static User getSingleUser(String email,String password)
{
User user = new User();
SQLiteDatabase db = DatabaseManager.getInstance().openDatabase();
String selectQuery = " SELECT * FROM " + User.TABLE + " WHERE "
+ User.KEY_EMAIL + "=?"+ " AND " + User.KEY_PASSWORD
+ "=?" ;
Cursor cursor = db.rawQuery(selectQuery,new String[]{email,password},null);
if (cursor.moveToFirst()) {
do {
user.setId(cursor.getInt(cursor.getColumnIndex("UserId")));
user.setFirstname(cursor.getString(cursor.getColumnIndex("Firstname")));
user.setLastname(cursor.getString(cursor.getColumnIndex("Lastname")));
user.setEmail(cursor.getString(cursor.getColumnIndex("Email")));
user.setPassword(cursor.getString(cursor.getColumnIndex("Password")));
user.setIsActive(cursor.getInt(cursor.getColumnIndex("IsActive")));
} while (cursor.moveToNext());
}
return user;
}
}
Try this,
String selection = User.KEY_EMAIL + "=?"+ " AND " + User.KEY_PASSWORD + "=?";
String[] selectionArgs = new String[]{ email, password };
Cursor cursor = db.query(User.TABLE, null, selection, selectionArgs, null, null, null);
try to use native android query as below -
Cursor cursor = db.query(User.TABLE, null, User.KEY_EMAIL + "=? and " + User.KEY_PASSWORD + "=?", new String[] {email, password}, null, null, null );
I am trying to retrieve contacts from my phone that has only numbers and put them into an arrayList, view them in lazy adapter and on click of name I would like show only numbers. I managed to get the list of contacts and numbers but the problem is when I have a contact with multiple numbers it just adds up into the list.
Something like for e.g
David +1 508 656 9043
David +1 403 604 7053
David +1 212 608 7053
Instead I would like to show only David in the list and when I click it should show all the three numbers.
I tried this:
void getContactNumbers()
{
ContentResolver cr = getContentResolver();
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME+ " COLLATE LOCALIZED ASC";
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, sortOrder);
if (cur.getCount() > 0)
{
while (cur.moveToNext())
{
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
Log.e("contact", "...Contact Name ...." + name);
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
{
Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
new String[]{id}, null);
while (pCur.moveToNext())
{
String phoneNo = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Log.e("contact", "...Contact Name ...." + name + "...contact Number..." + phoneNo);
}
pCur.close();
}
}
}
}
How to solve this part?
Thanks!
Thanks Harshid. There was selection change instead of IN_VISIBLE_GROUP + " = '1'"; -
I added HAS_PHONE_NUMBER + " = '1'";
All contacts came up.. Hope the below code helps others!!
Thanks!
final Uri uri = ContactsContract.Contacts.CONTENT_URI;
final String[] projection = new String[] {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.PHOTO_ID
};
String selection = ContactsContract.Contacts.HAS_PHONE_NUMBER + " = '1'";
final String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
Cursor cur = getContentResolver().query(uri, projection, selection, null, sortOrder);
if (cur.getCount() > 0)
{
while (cur.moveToNext())
{
String Sid = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
Log.e("contact", "...Contact Name ...." + name);
// get the phone number
Cursor pCur = getApplicationContext().getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = ?", new String[] { id }, null);
while (pCur.moveToNext()) {
number = pCur.getString(pCur .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
pCur.close();
}
}
cur.close();
You have to this way query and get contact with multiple number.
final Uri uri = ContactsContract.Contacts.CONTENT_URI;
final String[] projection = new String[] {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.PHOTO_URI
};
String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '1'";
String[] selectionArgs = null;
final String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
Cursor cur = getContentResolver().query(uri, projection, selection, selectionArgs, sortOrder);
if (cur.getCount() > 0)
{
while (cur.moveToNext())
{
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
Log.e("contact", "...Contact Name ...." + name);
// get the phone number
Cursor pCur = getApplicationContext().getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = ?", new String[] { id }, null);
while (pCur.moveToNext()) {
number = pCur.getString(pCur .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
pCur.close();
}
}
cur.close();
Try this code if getting error then put comment otherwise enjoy..
I have used following query to get phone numbers,display name and email
String[] PROJECTION = new String[] { Contacts.DISPLAY_NAME, Phone.NUMBER,
Email.DATA, Contacts._ID };
ContentResolver cr = getContentResolver();
Cursor cursor = cr.query(ContactsContract.Data.CONTENT_URI, PROJECTION,
null, null, null);
But not getting Phone number using
phonenumber = cursor
.getString(cursor
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER_ID));
Try with..
ContentResolver cr = getContentResolver();
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
"DISPLAY_NAME = '" + NAME + "'", null, null);
if (cursor.moveToFirst()) {
String contactId =
cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
//
// Get all phone numbers.
//
Cursor phones = cr.query(Phone.CONTENT_URI, null,
Phone.CONTACT_ID + " = " + contactId, null, null);
while (phones.moveToNext()) {
String number = phones.getString(phones.getColumnIndex(Phone.NUMBER));
int type = phones.getInt(phones.getColumnIndex(Phone.TYPE));
switch (type) {
case Phone.TYPE_HOME:
// do something with the Home number here...
break;
case Phone.TYPE_MOBILE:
// do something with the Mobile number here...
break;
case Phone.TYPE_WORK:
// do something with the Work number here...
break;
}
}
phones.close();
}
cursor.close();
Or,see..
http://www.higherpass.com/Android/Tutorials/Working-With-Android-Contacts/
Read all contact's phone numbers in android
i'm trying to read contacts with this code but it only gets Contacts but not contacts data.Please
tell me am i going on a wrong path or what is wrong with this.
Cursor contacts = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
while(contacts.moveToNext()) {
int contactIdColumnIndex = contacts.getColumnIndex(ContactsContract.Contacts._ID);
long contactId = contacts.getLong(contactIdColumnIndex);
System.out.println(contactId);
Cursor c = getContentResolver().query(Data.CONTENT_URI,
new String[] {Data._ID, Phone.NUMBER, Phone.TYPE},
Data.CONTACT_ID + "=?" + " AND "
+ Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE + "'",
new String[] {String.valueOf(contactId)}, null);
//retrieving data
while(c.moveToNext()){
long Id=c.getLong(0);
String number=c.getString(1);
String type=c.getString(2);
String name=c.getString(3);
System.out.println(Id);
System.out.println(number);
System.out.println(type);
System.out.println(name);
}
c.close();
}
contacts.close();
This runs well when i debug this and it only print ContactIds only
1
2
3
4
but no data...(apologize for long question)
try this code
String number = "";
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null,null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Cursor pCur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER},
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
new String[]{id}, null);
while (pCur.moveToNext()) {
for(int i=0;i<pCur.getColumnCount();i++)
number = pCur.getString(i);
}
pCur.close();
pCur = null;
}
}
}
cur.close();
cur = null;
cr = null;