Note that there are some explanatory texts on larger screens.

plurals
  1. POSave Modified Phone Number to ContactList at respective index Android
    primarykey
    data
    text
    <p>I successfully retrieved the position and phone number from my contact list. Now i need to save this number in my ContactList by modifying the orignal number. I am blocked at this step. please give me a hint example type position = 2 and number = 3049393. Here is my complete code.</p> <pre><code>public class MainActivity extends Activity { public static final int PICK_CONTACT = 1; TextView txt_contacts; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // this opens the activity. note the Intent.ACTION_GET_CONTENT // and the intent.setType ((Button)findViewById(R.id.btn_contacts)).setOnClickListener( new OnClickListener() { @Override public void onClick(View v) { // user BoD suggests using Intent.ACTION_PICK instead of .ACTION_GET_CONTENT to avoid the chooser Intent intent = new Intent(Intent.ACTION_GET_CONTENT); // BoD con't: CONTENT_TYPE instead of CONTENT_ITEM_TYPE intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE); startActivityForResult(intent, 1); txt_contacts =(TextView)findViewById(R.id.txt_contacts); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_main, menu); return true; } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (data != null) { Uri uri = data.getData(); if (uri != null) { Cursor c = null; try { c = getContentResolver().query(uri, new String[]{ ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.TYPE }, null, null, null); if (c != null &amp;&amp; c.moveToFirst()) { String number = c.getString(0); int type = c.getInt(1); showSelectedNumber(type, number); } } finally { if (c != null) { c.close(); } } } } } public void showSelectedNumber(int type, String number) { String new_num = 0+number; txt_contacts.setText(new_num); } public void updateContact() { try { ArrayList&lt;ContentProviderOperation&gt; ops = new ArrayList&lt;ContentProviderOperation&gt;(); ops.add(ContentProviderOperation.newUpdate(Data.CONTENT_URI) .withSelection(Data.RAW_CONTACT_ID + " = ?", new String[] {mRawContactId}) .withSelection(Data._ID + " = ?", new String[] {mDataId}) .withValue(Data.MIMETYPE, Email.CONTENT_ITEM_TYPE) .withValue(Data.DATA1, mEmail) .withValue(Email.TYPE, Email.TYPE_HOME) .build()); getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops); mResult.setText("Updated"); } catch (Exception e) { // Display warning Log.w("UpdateContact", e.getMessage()); for(StackTraceElement ste : e.getStackTrace()) { Log.w("UpdateContact", "\t" + ste.toString()); } Context ctx = getApplicationContext(); int duration = Toast.LENGTH_SHORT; Toast toast = Toast.makeText(ctx, "Update failed", duration); toast.show(); } } } </code></pre> <p><strong>Update :</strong></p> <p>public void showSelectedNumber(int type, String number) {</p> <pre><code> String new_num = 0+number; System.out.println("Display name:"+ number); ContentValues _changed_values=new ContentValues(); _changed_values.put(ContactsContract.CommonDataKinds.Phone.NUMBER,new_num); getContentResolver().update(uri,_changed_values,ContactsContract.CommonDataKinds.Phone._ID+"=?", new String[]{Integer.toString(type)}); // If you want to change the number txt_contacts.setText(new_num); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload