Phone number can not be inserted into "CONTACTS" in Android?


Last Updated:

  1. pay

    pay New Member This Topic's Starter

    Joined:
    Dec 25, 2009
    Messages:
    1
    Likes Received:
    0
    Hi all,
    I am new in Android.
    I am not able to add phone number into Contacts Application.
    Any help is very much appreciated in advance.
    -Pay:)


    -------------- Here is the java file --------
    package com.paad.Pay_USR_Phone;
    import java.util.ArrayList;
    import java.util.List;


    import android.app.Activity;
    import android.content.ContentResolver;
    import android.content.ContentValues;
    import android.content.Context;
    import android.content.Intent;
    import android.database.Cursor;
    import android.net.Uri;
    import android.os.Bundle;
    import android.provider.BaseColumns;
    import android.provider.Contacts;
    import android.provider.Contacts.People;
    import android.provider.Contacts.PeopleColumns;
    import android.provider.Contacts.PhonesColumns;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.LinearLayout;
    import android.widget.TextView;

    @SuppressWarnings("deprecation")
    public class Pay_USR_Phoneact extends Activity {
    /** Called when the activity is first created. */

    private EditText addName;
    private EditText addPhoneNumber;
    private EditText editName;


    private EditText editPhoneNumber;
    private Button addContact;
    private Button editContact;

    private long contactId;
    private class Contact {
    public long id;
    public String name;
    public String phoneNumber;
    public Contact (long id, String name, String phoneNumber){
    this.id = id;
    this.name = name;
    this.phoneNumber = phoneNumber;
    }
    @Override
    public String toString(){
    return this.name + "\n" + this.phoneNumber;

    }
    }

    private class ContactButton extends Button{
    public Contact contact;

    public ContactButton(Context ctx, Contact contact){
    super(ctx);
    this.contact = contact;
    }
    }


    @Override
    public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    this.setContentView(R.layout.usernamepasswordlayout);
    this.addName = (EditText) this.findViewById(R.id.add_name);
    this.addPhoneNumber = (EditText) this.findViewById(R.id.add_phone_number);
    this.editName = (EditText) this.findViewById(R.id.edit_name);
    this.editPhoneNumber = (EditText) this.findViewById(R.id.edit_phone_number);

    //Create anonymous click listener
    this.addContact = (Button) this.findViewById(R.id.add_contact_button);
    this.addContact.setOnClickListener(new OnClickListener() {

    public void onClick(final View v){
    Pay_USR_Phoneact.this.addContact();

    }
    });

    this.editContact = (Button) this.findViewById(R.id.edit_contact_button);
    this.editContact.setOnClickListener(new OnClickListener() {
    public void onClick(final View v) {
    Pay_USR_Phoneact.this.editContact();
    }
    });

    }


    //onStart portion of this application..
    @Override
    public void onStart() {
    super.onStart();
    List<Contact> contacts = this.getContacts();

    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(200,
    android.view.ViewGroup.LayoutParams.WRAP_CONTENT);

    if (contacts != null) {
    LinearLayout editLayout = (LinearLayout) this.findViewById(R.id.edit_buttons_layout);
    LinearLayout deleteLayout = (LinearLayout) this.findViewById(R.id.delete_buttons_layout);
    params.setMargins(10, 0, 0, 0);

    for (Contact c : contacts) {
    // Editing the result of saving..
    ContactButton contactEditButton = new ContactButton(this, c);
    contactEditButton.setText(c.toString());
    editLayout.addView(contactEditButton, params);
    contactEditButton.setOnClickListener(new OnClickListener() {

    public void onClick(final View v) {
    ContactButton view = (ContactButton) v;
    editName.setText(view.contact.name);


    editPhoneNumber.setText(view.contact.phoneNumber);
    contactId = view.contact.id;
    }
    });

    // Deleting the result of saving..

    ContactButton contactDeleteButton = new ContactButton(this, c);
    contactDeleteButton.setText("Delete " + c.name );

    deleteLayout.addView(contactDeleteButton, params);
    contactDeleteButton.setOnClickListener(new OnClickListener() {

    public void onClick(final View v) {
    ContactButton view = (ContactButton) v;
    contactId = view.contact.id;
    deleteContact();
    }
    });
    }
    }
    else

    {
    LinearLayout layout = (LinearLayout) this.findViewById(R.id.edit_buttons_layout);
    TextView empty = new TextView(this);
    empty.setText("No current contacts");
    layout.addView(empty, params);
    }
    }


    //Query portion of this application...
    private List<Contact> getContacts() {
    List<Contact> results = null;
    long id = 0L;
    String name = null;
    String phoneNumber = null;
    String[] projection = new String[]
    {
    Contacts.People._ID,
    Contacts.People.NAME,
    Contacts.People.NUMBER

    };

    ContentResolver resolver = this.getContentResolver();
    Cursor cur = resolver.query(Contacts.People.CONTENT_URI, projection, null, null,
    Contacts.People.DEFAULT_SORT_ORDER);

    while (cur.moveToNext()) {
    if (results == null) {
    results = new ArrayList<Contact>();
    }
    id = cur.getLong(cur.getColumnIndex(BaseColumns._ID));
    name = cur.getString(cur.getColumnIndex(PeopleColumns.NAME));
    phoneNumber = cur.getString(cur.getColumnIndex(PhonesColumns.NUMBER));
    results.add(new Contact(id, name, phoneNumber));
    results.add(new Contact(id, name,null));
    }
    return results;
    }

    // Insert portion of this Application..
    private void addContact() {
    ContentResolver resolver = getContentResolver();
    ContentValues values = new ContentValues();

    values.put(Contacts.People.NAME, this.addName.getText().toString());

    Uri personUri = Contacts.People.createPersonInMyContactsGroup(resolver, values);
    values.clear();
    Uri phoneUri = Uri.withAppendedPath(personUri, Contacts.People.Phones.CONTENT_DIRECTORY);
    values.put(Contacts.Phones.TYPE, PhonesColumns.TYPE_MOBILE);
    values.put(Contacts.Phones.NUMBER, this.addPhoneNumber.getText().toString());
    resolver.insert(phoneUri, values);

    this.startActivity(new Intent(this, Pay_USR_Phoneact.class));
    }

    // Edit-Result portion of this Application..
    private void editContact() {
    ContentResolver resolver = this.getContentResolver();
    ContentValues values = new ContentValues();
    Uri personUri = Contacts.People.CONTENT_URI.buildUpon().appendPath(Long.toString(this.contactId)).build();
    values.put(Contacts.People.NAME, this.editName.getText().toString());

    resolver.update(personUri, values, null, null);
    values.clear();

    Uri phoneUri = Uri.withAppendedPath(personUri, Contacts.People.Phones.CONTENT_DIRECTORY + "/1");
    values.put(Contacts.Phones.NUMBER, this.editPhoneNumber.getText().toString());
    resolver.update(phoneUri, values, null, null);

    this.startActivity(new Intent(this, Pay_USR_Phoneact.class));
    }


    // Delete-Result portion of this Application..
    private void deleteContact() {
    Uri personUri = Contacts.People.CONTENT_URI;
    personUri = personUri.buildUpon().appendPath(Long.toString(this.contactId)).build();
    getContentResolver().delete(personUri, null, null);

    startActivity(new Intent(this, Pay_USR_Phoneact.class));
    }


    }

    ---------------------------------------------------
     

    Advertisement
  2. ganeshpisal

    ganeshpisal Member

    Joined:
    Jun 2, 2010
    Messages:
    14
    Likes Received:
    0

Share This Page

Loading...