連絡先リストから位置と電話番号を正常に取得しました。ここで、元の番号を変更して、この番号を ContactList に保存する必要があります。この段階でブロックされました。type position = 2 and number = 3049393 のヒントの例を教えてください。これが私の完全なコードです。
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 && 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<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
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();
}
}
}
アップデート :
public void showSelectedNumber(int型, 文字列番号) {
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);
}