rawContact数据查询

来源:互联网 发布:Mac里Word隐藏段落标记 编辑:程序博客网 时间:2024/05/19 10:13
对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;
        }
    }
}
原创粉丝点击