Search code examples
androidandroid-contactssqliteopenhelper

How to read and store all the contacts to a list from contacts database?


I m trying to read the contacts from the contacts table and store all of them into a list variable..but not able to make out why it doesnt seem to work..gets a force close on execution.the contacts are needed to be send over a json object to a web server.

      private static final String TABLE_CONTACTS = "contacts";



// contacts JSONArray
JSONArray contacts = null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    List<contact> l=new ArrayList<contact>();
    String query="SELECT * FROM"+ TABLE_CONTACTS;

     DBHelper help= new DBHelper(getBaseContext()); 
    SQLiteDatabase db=help.getWritableDatabase();
    Cursor cursor = db.rawQuery(query, null);
    if(cursor.moveToFirst())
    {
        do{
            contact contact = new contact();
            contact.setID(Integer.parseInt(cursor.getString(0)));
            contact.setName(cursor.getString(1));
            contact.setPhoneNumber(cursor.getString(2));
            Toast.makeText(this,contact.name,Toast.LENGTH_LONG);
             l.add(contact);
        }
        while(cursor.moveToNext());
    }

and manifest file has 2 permissions

     <uses-permission android:name="android.permission.INTERNET" />
     <uses-permission android:name="android.permission.READ_CONTACTS"/>

and DBHelper class which is used to extend sqlitopenhelper to getWritableDatabase..

    class DBHelper extends SQLiteOpenHelper { 
private static final String DATABASE_NAME = "contactsManager";

   // Contacts table name
   private static final String TABLE_CONTACTS = "contacts";
DBHelper(Context context) { 
    super(context,DATABASE_NAME, null,1); 
}

@Override
public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub      
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO Auto-generated method stub      
}

}


Solution

  • private void getDetails(){
        Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
        ContentResolver cr = getContentResolver();
        Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
        String[] projection    = new String[] {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                            ContactsContract.CommonDataKinds.Phone.NUMBER };
        Cursor names = getContentResolver().query(uri, projection, null, null, null);
        int indexName = names.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
        int indexNumber = names.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
        names.moveToFirst();
        do {
           String name   = names.getString(indexName);
           Log.e("Name new:", name);
           String number = names.getString(indexNumber);
           Log.e("Number new:","::"+number);
        } while (names.moveToNext());
    }
    

    use the above code to get a list of name and numbers from contacts database.