1. Check out our companion app, Forums for Android! Download from Google Play

adding and deleting contact by code

Discussion in 'Application Development' started by adhavan, Aug 23, 2010.

  1. adhavan

    adhavan Member
    Thread Starter
    5

    Aug 23, 2010
    6
    0
    5
    hi,
    to display the contact i have used this coding
    Code (Text):
    1.  
    2. [LEFT][SIZE=2]Cursor cursor = getContacts();[/SIZE]
    3. [SIZE=2]String[] fields = [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] String[] { ContactsContract.Data.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]DISPLAY_NAME[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2] };[/SIZE]
    4. [SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]//this.setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_checked,));[/COLOR][/SIZE][/COLOR][/SIZE][/LEFT]
    5.  
    6. [SIZE=2]SimpleCursorAdapter adapter = [/SIZE]
    7. [LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] SimpleCursorAdapter([/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]this[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2],[/SIZE]
    8.  
    9. [SIZE=2]android.R.layout.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]simple_list_item_checked[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2], cursor, fields,[/SIZE]
    10. [LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]int[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2][] { R.id.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]contactEntryText[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2] });[/SIZE][/LEFT]
    11.  
    12.  
    13. [SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]mContactList[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].setAdapter(adapter);[/SIZE][/LEFT]
    14.  
    exception is thrown, i got successful in textview but i want it in listview(scrollable)​

    to add the contact, i have used the code ​
    Code (Text):
    1.  
    2.  
    3. [SIZE=2][LEFT]ArrayList<ContentProviderOperation> ops = [/LEFT]
    4. [/SIZE][LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] ArrayList<ContentProviderOperation>();[/SIZE]
    5. [LEFT][SIZE=2]ops.add(ContentProviderOperation.[I]newInsert[/I](Data.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]CONTENT_URI[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2])[/SIZE]
    6. [SIZE=2].withValue(Data.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]RAW_CONTACT_ID[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2],0)[/SIZE]
    7. [SIZE=2].withValue(Data.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]MIMETYPE[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2], Phone.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]CONTENT_ITEM_TYPE[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2])[/SIZE]
    8. [SIZE=2].withValue(Phone.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]NUMBER[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2], [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"1-800-GOOG-411"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2])[/SIZE]
    9. [SIZE=2].withValue(Phone.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]TYPE[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2], Phone.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]TYPE_CUSTOM[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2])[/SIZE]
    10. [SIZE=2].withValue(Phone.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]LABEL[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2], [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"free directory assistance"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2])[/SIZE][/LEFT]
    11.  
    12.  
    13. [SIZE=2].build());[/SIZE]
    14. [SIZE=2][COLOR=#7f0055][FONT=Courier New][B]try[/B][/FONT][/COLOR][COLOR=black][FONT=Courier New] {[/FONT][/COLOR][/SIZE]
    15. [SIZE=2][COLOR=black][FONT=Courier New]                getContentResolver().applyBatch(ContactsContract.[/FONT][/COLOR][I][COLOR=#0000c0][FONT=Courier New]AUTHORITY[/FONT][/COLOR][/I][COLOR=black][FONT=Courier New], ops);[/FONT][/COLOR][/SIZE]
    16. [SIZE=2][COLOR=black][FONT=Courier New]                toast = Toast.[I]makeText[/I](ctx,[/FONT][/COLOR][COLOR=#2a00ff][FONT=Courier New]"Deleted"[/FONT][/COLOR][COLOR=black][FONT=Courier New], duration);[/FONT][/COLOR][/SIZE]
    17. [SIZE=2][COLOR=black][FONT=Courier New]                toast.show();[/FONT][/COLOR][/SIZE]
    18. [SIZE=2][COLOR=black][FONT=Courier New]                populateContactList();[/FONT][/COLOR][/SIZE][/LEFT]
    19.  
    20. [LEFT][SIZE=2][COLOR=black][FONT=Courier New]            } [/FONT][/COLOR][B][COLOR=#7f0055][FONT=Courier New]catch[/FONT][/COLOR][/B][COLOR=black][FONT=Courier New] (Exception e) {[/FONT][/COLOR][/SIZE][/LEFT]
    21.  
    22. [LEFT][SIZE=2][COLOR=black][FONT=Courier New]                  Log.[I]e[/I]([/FONT][/COLOR][I][COLOR=#0000c0][FONT=Courier New]TAG[/FONT][/COLOR][/I][COLOR=black][FONT=Courier New], [/FONT][/COLOR][COLOR=#2a00ff][FONT=Courier New]"Exceptoin encoutered while deleting contact: "[/FONT][/COLOR][COLOR=black][FONT=Courier New] + e);[/FONT][/COLOR][/SIZE]
    23. [SIZE=2][COLOR=black][FONT=Courier New]            }[/FONT][/COLOR][/SIZE][/LEFT]
    24.  
    to delete i used this coding​
    Code (Text):
    1.  
    2. [LEFT][COLOR=black][FONT=Courier New]            ArrayList<ContentProviderOperation> ops = [/FONT][/COLOR][B][COLOR=#7f0055][FONT=Courier New]new[/FONT][/COLOR][/B][COLOR=black][FONT=Courier New] ArrayList<ContentProviderOperation>();[/FONT][/COLOR]
    3. [COLOR=black][FONT=Courier New]            ops.add(ContentProviderOperation.[I]newDelete[/I](Data.[/FONT][/COLOR][I][COLOR=#0000c0][FONT=Courier New]CONTENT_URI[/FONT][/COLOR][/I][COLOR=black][FONT=Courier New])[/FONT][/COLOR]
    4. [COLOR=black][FONT=Courier New]                     .withSelection(Data.[/FONT][/COLOR][I][COLOR=#0000c0][FONT=Courier New]_ID[/FONT][/COLOR][/I][COLOR=black][FONT=Courier New] + [/FONT][/COLOR][COLOR=#2a00ff][FONT=Courier New]"=?"[/FONT][/COLOR][COLOR=black][FONT=Courier New], [/FONT][/COLOR][B][COLOR=#7f0055][FONT=Courier New]new[/FONT][/COLOR][/B][COLOR=black][FONT=Courier New] String[]{String.[I]valueOf[/I](name)})[/FONT][/COLOR]
    5. [COLOR=black][FONT=Courier New]                     .build());[/FONT][/COLOR]
    6. [B][COLOR=#7f0055][FONT=Courier New]try[/FONT][/COLOR][/B][COLOR=black][FONT=Courier New] {[/FONT][/COLOR]
    7. [COLOR=black][FONT=Courier New]                getContentResolver().applyBatch(ContactsContract.[/FONT][/COLOR][I][COLOR=#0000c0][FONT=Courier New]AUTHORITY[/FONT][/COLOR][/I][COLOR=black][FONT=Courier New], ops);[/FONT][/COLOR]
    8. [COLOR=black][FONT=Courier New]                toast = Toast.[I]makeText[/I](ctx,[/FONT][/COLOR][COLOR=#2a00ff][FONT=Courier New]"Deleted"[/FONT][/COLOR][COLOR=black][FONT=Courier New], duration);[/FONT][/COLOR]
    9. [COLOR=black][FONT=Courier New]                toast.show();[/FONT][/COLOR]
    10. [COLOR=black][FONT=Courier New]                populateContactList();[/FONT][/COLOR][/LEFT]
    11.  
    12. [LEFT][COLOR=black][FONT=Courier New]            } [/FONT][/COLOR][B][COLOR=#7f0055][FONT=Courier New]catch[/FONT][/COLOR][/B][COLOR=black][FONT=Courier New] (Exception e) {[/FONT][/COLOR][/LEFT]
    13.  
    14. [LEFT][COLOR=black][FONT=Courier New]                  Log.[I]e[/I]([/FONT][/COLOR][I][COLOR=#0000c0][FONT=Courier New]TAG[/FONT][/COLOR][/I][COLOR=black][FONT=Courier New], [/FONT][/COLOR][COLOR=#2a00ff][FONT=Courier New]"Exceptoin encoutered while deleting contact: "[/FONT][/COLOR][COLOR=black][FONT=Courier New] + e);[/FONT][/COLOR]
    15. [COLOR=black][FONT=Courier New]            }[/FONT][/COLOR][/LEFT]
    16.  
    i both add and delete i am not getting any error, but it is not reflected nither in the contact nor in the textView.
    please help me , i am a newbie.​
     

    Advertisement

  2. star007

    star007 New Member
    25

    Dec 27, 2011
    3
    0
    25
    Hi All,
    I know the code below opens the native Edit/Add contact screen

    Intent newIntent = new Intent(Intent.ACTION_INSERT,
    ContactsContract.Contacts.CONTENT_URI);
    startActivityForResult(newIntent, 1);

    now i want this screen to be populated with some data in fields like
    name, phone number, address etc. I found that if i do something like
    newIntent.putExtra(ContactsContract.Intents.Insert .NAME, "Some
    Name");
    will show this screen with name "Some Name" being shown in the name
    field this is fine for me now i dont understand how will i fill the
    first name, last name , city, state and zip fields similarly.
    I found the ContactsContract.Intents.Insert has a
    POSTAL attribute for address but there are no specific attributes for
    first name, last name, city, state and zip in ContactsContract.Intents.Insert.
    Also i found that
    ContactsContract.CommonDataKinds.StructuredPostal has the first name, last name, city, state
    and zip attributes but still i dont know how can i use this to
    prepopulate the Edit/Add contact screen with values of first name, last name, city, state
    and zip.

    Please Help me with this,
    star
     

Share This Page

Loading...