2014-03-31 170 views
-1

我曾嘗試將以下代碼存儲在android電話簿中,但失敗。 我發佈了我試過的代碼,試圖在Android聯繫人中存儲聯繫人條目

請幫忙。 謝謝。

@Override 
public void onClick(View v) { 
    String nm, num; 
    nm = t1.getText().toString(); 
    num = t2.getText().toString(); 
    ContentResolver c = getContentResolver(); 
    ContentValues record = new ContentValues(); 
    int iddd = TotalCount + 1; 
    record.put("Contacts._ID", iddd); 
    record.put("Contacts.DISPLAY_NAME", nm); 
    record.put("Contacts.HAS_PHONE_NUMBER", 1); 
    try { 
     c.insert(ContactsContract.Contacts.CONTENT_URI, record); 
     ContentValues record1 = new ContentValues(); 
     record1.put("CommonDataKinds.Phone.NUMBER", num); 
     c.insert(CommonDataKinds.Phone.CONTENT_URI, record1); 
     Toast.makeText(this, "Contact saved2", Toast.LENGTH_LONG).show(); 
    } catch (Exception e) { 
     Toast.makeText(this, "Failed to save contact", Toast.LENGTH_LONG) 
       .show(); 
    } 
} 
    This code when executed, gives an error. Failed to insert contact. 
    Minimum sdk version is 8. 
    Target sdk is 18. 

    But don't know why it's causing this error. 

回答

0

您可以嘗試通過存儲在ArrayList中的聯繫電話和聯繫人姓名與另一個ArrayList中存儲從您的電話與我們聯繫驗證碼:

=== Contacts.java ===

package com.showcontacts;

import java.util.ArrayList;

import android.app.Activity;

import android.content.ContentResolver;

import android.database.Cursor;

import android.os.Bundle;

import android.provider.ContactsContract;

import android.util.Log;

import android.view.Menu;

import android.view.View;

public class Contacts extends Activity { 

String phoneNumber; 

ArrayList<String> phones = new ArrayList<String>(); 
ArrayList<String> contact_name = new ArrayList<String>(); 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_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.contacts, menu); 
    return true; 
} 

/**** get all contacts ******/ 
public void getNumber(View v) { 
    ContentResolver cr = getContentResolver(); 
    String name; 
    Cursor cursor = cr.query(
      ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, 
      null, null); 
    while (cursor.moveToNext()) { 
     name = cursor 
       .getString(cursor 
         .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); 
     phoneNumber = cursor 
       .getString(cursor 
         .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 
     phones.add(phoneNumber); 
     contact_name.add(name);// fill the Arraylist 
    } 
    cursor.close();// close cursor 

    for (int i = 0; i < phones.size(); i++) { 
     Log.d(contact_name.get(i) + i, (phones.get(i))); 

    } 
} 

}

** ** activity_contacts.xml

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context=".Contacts" > 
<Button 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="26dp" 
    android:text="Button" 
    android:onClick="getNumber" 
    /> 

**不要忘了在你的清單**

<uses-permission android:name="android.permission.READ_CONTACTS"/> 

最佳添加權限祝你好運