rawContact資料查詢

來源:互聯網
上載者:User

對RawContacts主表可查詢以下共18項資料:

變數名                                列名                                備忘

 _ID                                _id                                    這個就是raw_contact_id

 CONTACT_ID                        contact_id

 AGGREGATION_MODE                  aggregation_mode                        分組模式

 DELETED                               deleted                                是否已經被刪除

 TIMES_CONTACTED                times_contacted

 LAST_TIME_CONTACTED            last_time_contacted

 STARRED                            starred

 CUSTOM_RINGTONE                    custom_ringtone

 SEND_TO_VOICEMAIL                    send_to_voicemail

 ACCOUNT_NAME                        account_name

 ACCOUNT_TYPE                        account_type

 SOURCE_ID                            sourceid

 VERSION                            version

 DIRTY                                dirty

 SYNC1~SYNC1                        sync1~sync2

 注意1: _ID就是raw_contact_id

 注意2:當我們查詢時,通常是查詢沒有刪除的連絡人。所以要加上條件RawContacts.DELETED==0:

 RawContacts.DELETED + "=?", new String[] {String.valueOf(0)}
 注意3:主表中沒有名字的資訊.名字在子表StructuredName中。

 查詢例1:

         String[] projection = new String[] {

                RawContacts._ID,

                RawContacts.CONTACT_ID,

                };

        Cursor contactData = managedQuery(

                RawContacts.CONTENT_URI,

                null, 

                RawContacts.DELETED + "=?", 

                new String[] {String.valueOf(0)},

                null);

 查詢例2:

         Cursor c = managedQuery(

                ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId),

                projection, 

                null, 

                null, 

                null);  
注意:這裡是通過ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId)

產生於rawContactId相對應的URL來進行查詢。
查詢例3:

 To find raw contacts within a specific account, you can either put the account name and type in the selection or pass them as query parameters.

 The latter approach is preferable, especially when you can reuse the URI:

這樣的話就可以重複利用URL,不用每次都要在SQL語句中指定RawContacts.ACCOUNT_NAME,RawContacts.ACCOUNT_TYPE

 Uri rawContactUri = RawContacts.URI.buildUpon()

          .appendQueryParameter(RawContacts.ACCOUNT_NAME, accountName)

          .appendQueryParameter(RawContacts.ACCOUNT_TYPE, accountType)

          .build();

 Cursor c1 = getContentResolver().query(rawContactUri,

          RawContacts.STARRED + "<>0", null, null, null);//RawContacts.STARRED不為0

 ...

 Cursor c2 = getContentResolver().query(rawContactUri,

          RawContacts.DELETED + "<>0", null, null, null);
查詢例4:

The best way to read a raw contact along with all the data associated with it is by using the ContactsContract.RawContacts.Entity directory. 

If the raw contact has data rows, the Entity cursor will contain a row for each data row. If the raw contact has no data rows,

the cursor will still contain one row with the raw contact-level information.

Uri rawContactUri = ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId);

Uri entityUri = Uri.withAppendedPath(rawContactUri, Entity.CONTENT_DIRECTORY);

Cursor c = getContentResolver().query(entityUri,

          new String[]{RawContacts.SOURCE_ID, Entity.DATA_ID, Entity.MIMETYPE, Entity.DATA1},

          null, null, null);

 try {

     while (c.moveToNext()) {

         String sourceId = c.getString(0);

         if (!c.isNull(1)) {

             String mimeType = c.getString(2);

             String data = c.getString(3);

             ...

         }

     }

 } finally {

     c.close();

 }
在主表中查詢不到name資訊,但是可以通過子表StructuredName來得。

而且在對子表進行查詢時,它會把主表的11項資料連結過來。一般情況下,這些資料已經夠用了。

關於子表查詢的更多資訊可參考《raw contact子表資料查詢

下面這個程式將展示如何顯示每個人的電話號碼。
執行個體1:

package com.teleca;

import android.app.ExpandableListActivity;

import android.content.ContentUris;

import android.content.Context;

import android.database.Cursor;

import android.net.Uri;

import android.os.Bundle;

import android.provider.ContactsContract.RawContacts;

import android.provider.ContactsContract.CommonDataKinds.Phone;

import android.provider.ContactsContract.CommonDataKinds.StructuredName;

import android.provider.ContactsContract.CommonDataKinds.Email;

import android.util.Log;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.view.ViewGroup.LayoutParams;

import android.widget.CursorTreeAdapter;

import android.widget.ExpandableListAdapter;

import android.widget.ExpandableListView;

import android.widget.TextView;

import android.widget.Toast;

import android.provider.ContactsContract.Data;
public class CursorTreeAdapterActivity extends ExpandableListActivity {

    private final String TAG = "hubin"; 

    ExpandableListView mExpandableList;

    ExpandableListAdapter mAdapter;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        

        // Get the expandable list view object

        mExpandableList = getExpandableListView();

        

        // Set our expandable list adapter

        String[] projection = new String[] {

                StructuredName.DISPLAY_NAME,

                StructuredName.RAW_CONTACT_ID,

                };

        Cursor contactData = managedQuery(

                Data.CONTENT_URI,

                null, 

                Data.MIMETYPE + "='" + StructuredName.CONTENT_ITEM_TYPE + "'", 

                null,

                null);

        mAdapter = new CursorTreeAdapterExample(contactData, this);

        setListAdapter(mAdapter);

    }

    class CursorTreeAdapterExample extends CursorTreeAdapter {

        private int mGroupIdColumnIndex;

        private LayoutInflater mInflater;

        private Context context;

        public CursorTreeAdapterExample(Cursor cursor, Context context) {

            super(cursor, context);

            

            mGroupIdColumnIndex = cursor.getColumnIndexOrThrow(StructuredName.RAW_CONTACT_ID);

            mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        }

        @Override

        protected void bindChildView(View view, Context context, Cursor cursor, boolean isExpanded) {

            // Bind the related data with this child view

            ((TextView)view).setText(cursor.getString(cursor.getColumnIndex(Phone.NUMBER)));

        }

        @Override

        protected void bindGroupView(View view, Context context, Cursor cursor, boolean isExpanded) {

            // Bind the related data with this group view

            ((TextView)view).setText(cursor.getString(cursor.getColumnIndex(StructuredName.DISPLAY_NAME)));

        }

        @Override

        protected Cursor getChildrenCursor(Cursor groupCursor) {

            Long rawContactId=groupCursor.getLong(mGroupIdColumnIndex);

            Log.i(TAG,"gg:"+rawContactId);

            Cursor c=managedQuery(Data.CONTENT_URI, new String[] {Phone._ID, Phone.NUMBER},

                    Data.RAW_CONTACT_ID + "=?"+" AND "

                      + Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE + "'",

                      new String[] {String.valueOf(rawContactId)}, null);

            return c;

        }

        @Override

        protected View newChildView(Context context, Cursor cursor, boolean isExpanded, ViewGroup parent) {

            Log.d(TAG, "newChildView");

            

            TextView view = (TextView) mInflater.inflate(android.R.layout.simple_expandable_list_item_1, parent, false);        

            return view;

        }

        @Override

        protected View newGroupView(Context context, Cursor cursor, boolean isExpanded, ViewGroup parent) {

            Log.d(TAG, "newGroupView");

            TextView view = (TextView) mInflater.inflate(android.R.layout.simple_expandable_list_item_1, parent, false);

            return view;

        }

    }

}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.