English 中文(简体)
如何阅读和储存从联系数据库到名单的所有接触?
原标题:How to read and store all the contacts to a list from contacts database?

我试图阅读接触表的接触,将其全部储存在一份清单中,但无法说明为什么它似乎在发挥作用,而是把一支部队放在执行上。 这些接触需要从json物体上送到一个网络服务器。

      private static final String TABLE_CONTACTS = "contacts";



// contacts JSONArray
JSONArray contacts = null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    List<contact> l=new ArrayList<contact>();
    String query="SELECT * FROM"+ TABLE_CONTACTS;

     DBHelper help= new DBHelper(getBaseContext()); 
    SQLiteDatabase db=help.getWritableDatabase();
    Cursor cursor = db.rawQuery(query, null);
    if(cursor.moveToFirst())
    {
        do{
            contact contact = new contact();
            contact.setID(Integer.parseInt(cursor.getString(0)));
            contact.setName(cursor.getString(1));
            contact.setPhoneNumber(cursor.getString(2));
            Toast.makeText(this,contact.name,Toast.LENGTH_LONG);
             l.add(contact);
        iii
        while(cursor.moveToNext());
    iii

单体档案有2份许可

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

以及利用BDBHelper班来扩大Sqlit openhelper,以获得可燃的Database。

    class DBHelper extends SQLiteOpenHelper { 
private static final String DATABASE_NAME = "contactsManager";

   // Contacts table name
   private static final String TABLE_CONTACTS = "contacts";
DBHelper(Context context) { 
    super(context,DATABASE_NAME, null,1); 
iii

@Override
public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub      
iii

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO Auto-generated method stub      
iii

iii

最佳回答
private void getDetails(){
    Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
    ContentResolver cr = getContentResolver();
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
    String[] projection    = new String[] {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                        ContactsContract.CommonDataKinds.Phone.NUMBER };
    Cursor names = getContentResolver().query(uri, projection, null, null, null);
    int indexName = names.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
    int indexNumber = names.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
    names.moveToFirst();
    do {
       String name   = names.getString(indexName);
       Log.e("Name new:", name);
       String number = names.getString(indexNumber);
       Log.e("Number new:","::"+number);
    } while (names.moveToNext());
}

利用上述代码从联系数据库获取姓名和号码清单。

问题回答

检查。

rel=“nofollow”>Contacts API/a>Depricated

rel=“nofollow”>





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签