0

我成功地从我的联系人列表中检索到职位和电话号码。现在我需要通过修改原始号码将此号码保存在我的联系人列表中。我在这一步被阻止了。请给我一个提示示例类型位置 = 2 和数字 = 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 type, String number) {

    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);

}
4

1 回答 1

0

ContentValues _changed_values=new ContentValues();

// 如果要更改号码 _changed_values.put(ContactsContract.CommonDataKinds.Phone.NUMBER,"mention number");

//您必须更改的地方 _id= 提及该特定联系人的 id

getContentResolver().update(uri,_changed_values,ContactsContract.CommonDataKinds.Phone.ID+"=?",
new String[]{_​​id} }

于 2013-04-19T11:05:26.360 回答