title: add only contact name and number to android and display in textview at runtime
body: <p>i am trying to insert the contact name and number to contacts in android and trying view the same in edit view..
  like to show you the code..</p>
  
  <p><strong>updated content [by myself]:</strong>
  Go through following link for answer;
  <a href="http://taaniapps.blogspot.in/2013/06/demo-for-four-major-components-of_17.html" rel="nofollow noreferrer">http://taaniapps.blogspot.in/2013/06/demo-for-four-major-components-of_17.html</a></p>
  
  <p>CODE:</p>
  
  <pre><code>import java.util.ArrayList;
  import android.app.Activity;
  import android.content.ContentProviderOperation;
  import android.content.ContentValues;
  import android.content.OperationApplicationException;
  import android.database.Cursor;
  import android.net.Uri;
  import android.os.Bundle;
  import android.os.RemoteException;
  import android.provider.ContactsContract;
  import android.view.View;
  import android.widget.EditText;
  import android.widget.TextView;
  import android.widget.Toast;
  
  public class ContentProviderContactsDemo extends Activity
  {
  
  ContentValues values = new ContentValues(); 
  
  // for viewing existing contact name and number
  TextView name, number;
  
  // for entering new contact name and number on add button click
  EditText newname, newnumber;
  
  // Form an array specifying which columns to return. 
  String[] projection = new String[] {
          ContactsContract.Contacts.DISPLAY_NAME,
          ContactsContract.CommonDataKinds.Phone.NUMBER 
  };
  
  // Get the base URI for the People/Contacts table in the Contacts content provider.
  Uri contacts = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      // TODO Auto-generated method stub
      super.onCreate(savedInstanceState);
      setContentView(R.layout.contacts_display);
  
      name = (TextView) findViewById(R.id.name);
      number = (TextView) findViewById(R.id.number);
  
      newname = (EditText) findViewById(R.id.newname);
      newnumber = (EditText) findViewById(R.id.newnumber);
  
      makeQuery();
  }
  
  private void makeQuery()
  {
      // Make the query. 
      Cursor managedCursor = getContentResolver().query(contacts,
              projection,       // Which columns to return 
              null,            // Which rows to return (all rows)
              null,           // Selection arguments (none)
              null           // Put the results in ascending order by name
              );
  
      name.append("\n" + "NAME \n");
      number.append("\n" + "PHONE NUMBERS \n");
  
      // display records in TextView
      getColumnData(managedCursor);
  }
  
  private void getColumnData(Cursor cur)
  { 
      String name_disp; 
      String phoneNumber_disp; 
  
      //moving the cursor to the first record or row data
      cur.moveToFirst();
      int nameColumn = cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME); 
      int phoneColumn = cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
      do
      {
          // Get the field values and display it in a TextView
          //get Names
          name_disp = cur.getString(nameColumn);
          //insert it in TextView
          name.append("\n" + name_disp);
          //get phone number
          phoneNumber_disp = cur.getString(phoneColumn);
          //insert it in TextView
          number.append("\n" + phoneNumber_disp);
      } while (cur.moveToNext());
  }
  
  public void insertData(View v) throws RemoteException, OperationApplicationException
  {
      String addname;
      String addnumber;
  
      addname = newname.getText().toString();
      addnumber = newnumber.getText().toString();
  
      //// one way to insert the new contact name and number ERROR !!!
  
      // Add Records to contacts and make him a favorite.
      //values.put(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, addname);
      //values.put(Phone.NUMBER, addnumber);
  
      //Uri uri = getContentResolver().insert(ContactsContract.Data.CONTENT_URI, values);
      // returns the full URI for the added record, meaning with the ID
  
  
      //// other way to insert the contact name
      ArrayList&lt;ContentProviderOperation&gt; ops = new ArrayList&lt;ContentProviderOperation&gt;();
  
      if (addname != null) 
      {
          ops.add(ContentProviderOperation.newInsert( ContactsContract.Data.CONTENT_URI)
                  .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
                  .withValue(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
                  .withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME,addname)
                  .build());
      }
  
      if (addnumber != null) 
      {
          ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                  .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
                  .withValue(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
                  .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, addnumber)
                  .withValue(ContactsContract.CommonDataKinds.Phone.TYPE,ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE)
                  .build());
      }
  
      try 
      {
          getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
      } 
      catch (Exception e) 
      {
          e.printStackTrace();
          Toast.makeText(this, "Exception: " + e.getMessage(), Toast.LENGTH_SHORT).show();
      }
  
      // clearing list of contact display and displaying with updated contact name and number
  
      name.setText("");
      number.setText("");
  
      makeQuery();
  }
  }
  </code></pre>
  
  <p>i have added comments to make you understand the code.
  what i want is to list contacts of the newly inserted recorded..
  i am also not sure whether my code insert the new record in contact because i am unable view it.</p>
  
  <p>show you the screen shot for layout file..</p>
  
  <p><img src="https://i.stack.imgur.com/ZtsLJ.png" alt="layout file"></p>
  
  <p>as you see int the layout the contacts display above are already added contact in the android. but when i add new contact i am unable to see the updated view.</p>
  
  <p>please any one can help me on this..
  i am not sure but i think when i press on ADD button contact is actually not getting add..so it is not updating or showing in above TextView..
  i have refer most of the post every where in stack overflow and other sites too..but i am still unable to find any right answer to insert the new contact name and number in android..</p>
  
  <p>Please help me..</p>