0

我正在尝试将联系人姓名和号码插入 android 中的联系人并尝试在编辑视图中查看相同的内容.. 想向您展示代码..

更新内容[我自己]: 通过以下链接获取答案; http://taaniapps.blogspot.in/2013/06/demo-for-four-major-components-of_17.html

代码:

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<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();

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

我添加了注释以使您理解代码。我想要的是列出新插入记录的联系人。我也不确定我的代码是否在联系人中插入了新记录,因为我无法查看它。

向您展示布局文件的屏幕截图..

布局文件

正如您在布局中看到的那样,上面显示的联系人已经在 android 中添加了联系人。但是当我添加新联系人时,我无法看到更新的视图。

请任何人都可以帮助我..我不确定,但我认为当我按下添加按钮时,联系人实际上没有得到添加..所以它没有更新或显示在上面的 TextView.. 我参考了大部分帖子堆栈溢出和其他站点中的每个位置也是如此..但我仍然无法找到任何正确的答案来在 android 中插入新的联系人姓名和号码..

请帮我..

4

1 回答 1

0

如果要添加联系人,请使用以下代码。private void addContact(字符串名称,字符串电话,字符串电子邮件,字符串传真){ ArrayList ops = new ArrayList(); int rawContactInsertIndex = ops.size();

    ops.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI)
            .withValue(RawContacts.ACCOUNT_TYPE, null)
            .withValue(RawContacts.ACCOUNT_NAME, null).build());
    ops.add(ContentProviderOperation
            .newInsert(Data.CONTENT_URI)
            .withValueBackReference(Data.RAW_CONTACT_ID,
                    rawContactInsertIndex)
            .withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE)
            .withValue(StructuredName.DISPLAY_NAME, name).build());
    ops.add(ContentProviderOperation
            .newInsert(Data.CONTENT_URI)
            .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID,
                    rawContactInsertIndex)
            .withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE)
            .withValue(Phone.NUMBER, fax)
            .withValue(Phone.TYPE, Phone.TYPE_FAX_WORK).build());
    ops.add(ContentProviderOperation
            .newInsert(Data.CONTENT_URI)
            .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID,
                    rawContactInsertIndex)
            .withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE)
            .withValue(Phone.NUMBER, phone)
            .withValue(Phone.TYPE, Phone.TYPE_MOBILE).build());
    ops.add(ContentProviderOperation
            .newInsert(Data.CONTENT_URI)
            .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
            .withValue(Data.MIMETYPE, Email.CONTENT_ITEM_TYPE)
            .withValue(ContactsContract.CommonDataKinds.Email.DATA, email)
            .withValue(Email.TYPE, Email.TYPE_WORK).build());

    try {
        ContentProviderResult[] res = getContentResolver().applyBatch(
                ContactsContract.AUTHORITY, ops);
        System.out.println("" + res);
    } catch (RemoteException e) {
        e.printStackTrace();
    } catch (OperationApplicationException e) {
        e.printStackTrace();
    }
}
于 2013-06-17T10:13:29.557 回答