• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps adding and deleting contact by code

adhavan

Lurker
hi,
to display the contact i have used this coding
Code:
[LEFT][SIZE=2]Cursor cursor = getContacts();[/SIZE]
[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]
[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]
 
[SIZE=2]SimpleCursorAdapter adapter = [/SIZE]
[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]

[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]
[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]


[SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]mContactList[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].setAdapter(adapter);[/SIZE][/LEFT]

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:
[SIZE=2][LEFT]ArrayList<ContentProviderOperation> ops = [/LEFT]
[/SIZE][LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] ArrayList<ContentProviderOperation>();[/SIZE]
[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]
[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]
[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]
[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]
[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]
[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]


[SIZE=2].build());[/SIZE]
[SIZE=2][COLOR=#7f0055][FONT=Courier New][B]try[/B][/FONT][/COLOR][COLOR=black][FONT=Courier New] {[/FONT][/COLOR][/SIZE]
[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]
[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]
[SIZE=2][COLOR=black][FONT=Courier New]                toast.show();[/FONT][/COLOR][/SIZE]
[SIZE=2][COLOR=black][FONT=Courier New]                populateContactList();[/FONT][/COLOR][/SIZE][/LEFT]
 
[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]
 
[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]
[SIZE=2][COLOR=black][FONT=Courier New]            }[/FONT][/COLOR][/SIZE][/LEFT]

to delete i used this coding​
Code:
[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]
[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]
[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]
[COLOR=black][FONT=Courier New]                     .build());[/FONT][/COLOR]
[B][COLOR=#7f0055][FONT=Courier New]try[/FONT][/COLOR][/B][COLOR=black][FONT=Courier New] {[/FONT][/COLOR]
[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]
[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]
[COLOR=black][FONT=Courier New]                toast.show();[/FONT][/COLOR]
[COLOR=black][FONT=Courier New]                populateContactList();[/FONT][/COLOR][/LEFT]
 
[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]
 
[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]
[COLOR=black][FONT=Courier New]            }[/FONT][/COLOR][/LEFT]

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.​
 
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
 
Back
Top Bottom