ContentResolver的使用_夏天的风—andy的空间_百度空间

         这里借助了AutoCompleteView Widget控件,已达到建议和提示的功能。运行此程序时,需要向通讯录输入一些数据,否则会造成空指针的错误。我们要达到的最终效果即是输入一个单词a,马上会将以a开头的所有名字显示出来。

     本例重点:1,如何取得通讯录的数据

                       2,如何将上面获取的数据显示出来

                       3,完成以上Auto的功能

       对于{dy}点,ContentResolver的query(Uri uri,String[] projection,String selection,String[] selectionArgs,String sortOrder),返回的就是 一个Cursor,且Cursor的column index从0开始,注意java.sql.ResultSet是从1开始。

      而要取得通讯录的数据,则是使用ContentResolver对象,以content.query的方法取得所有通讯录里的联系人,并以Cursor的方式取得其存储内容。

   具体源码如下:

     package com.allove;


import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.ContentResolver;
import android.content.Context;
import android.content.DialogInterface;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.Contacts;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AutoCompleteTextView;
import android.widget.CursorAdapter;
import android.widget.TextView;

public class ContentResolverTest extends ListActivity {
private AutoCompleteTextView mAutoCompleteTextView;
private Cursor mContactCursor;
private ContactAdapter mContactAdapter;
// 设置要取得通讯录的通讯录
private static final String[] PEOPLE_PROJECTION = new String[] {
Contacts.People._ID, Contacts.People.PRIMARY_PHONE_ID,
Contacts.People.TYPE, Contacts.People.NUMBER,
Contacts.People.LABEL, Contacts.People.NAME };

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

mAutoCompleteTextView = (AutoCompleteTextView) this
.findViewById(R.id.AutoCompleteTextView01);

ContentResolver cr = this.getContentResolver();
mContactCursor = cr.query(Contacts.People.CONTENT_URI,
PEOPLE_PROJECTION, null, null,
Contacts.People.DEFAULT_SORT_ORDER);

mContactAdapter = new ContactAdapter(this, mContactCursor);

mAutoCompleteTextView.setAdapter(mContactAdapter);

mAutoCompleteTextView
.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// 取得cursor
Cursor c = mContactAdapter.getCursor();
// 移到所单击的位置
c.moveToPosition(arg2);
String number = c.getString(c
.getColumnIndexOrThrow(Contacts.People.NUMBER));
// 当找不到电话号码时
number = number == null ? "无输入电话" : number;
new AlertDialog.Builder(ContentResolverTest.this)
.setTitle(
c
.getColumnIndexOrThrow(Contacts.People.NAME)
+ "的电话是" + number)
.setPositiveButton("确定",
new DialogInterface.OnClickListener() {

@Override
public void onClick(
DialogInterface dialog,
int which) {
// TODO Auto-generated method
// stub

}
});
}
});
}

class ContactAdapter extends CursorAdapter {
private ContentResolver mContent;

public ContactAdapter(Context context, Cursor c) {
super(context, c);
mContent = context.getContentResolver();
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
// TODO Auto-generated method stub
((TextView) view).setText(cursor.getString(cursor
.getColumnIndexOrThrow(Contacts.People.NAME)));
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
// TODO Auto-generated method stub
final LayoutInflater inflater = LayoutInflater.from(context);
final TextView view = (TextView) inflater.inflate(
android.R.layout.simple_dropdown_item_1line, parent, false);
view.setText(cursor.getString(cursor
.getColumnIndexOrThrow(Contacts.People.NAME)));
return view;
}

@Override
public CharSequence convertToString(Cursor cursor) {
// TODO Auto-generated method stub
return cursor.getString(cursor
.getColumnIndexOrThrow(Contacts.People.NAME));
}

@Override
public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
// TODO Auto-generated method stub
if (this.getFilterQueryProvider() != null) {
return this.getFilterQueryProvider().runQuery(constraint);
}
StringBuilder buffer = null;
String[] s = null;
if (constraint != null) {
buffer.append("UPPER(");
buffer.append(Contacts.People.NAME);
buffer.append(") GLOB?");
s = new String[] { constraint.toString().toUpperCase() + "*" };
}

return mContent.query(Contacts.CONTENT_URI,
ContentResolverTest.PEOPLE_PROJECTION,
buffer == null ? null : buffer.toString(), s,
Contacts.People.DEFAULT_SORT_ORDER);
}

}
}

此外不要忘记在AndroidManifest.xml中加入权限——Android.permission.READ_CONTACTS



郑重声明:资讯 【ContentResolver的使用_夏天的风—andy的空间_百度空间】由 发布,版权归原作者及其所在单位,其原创性以及文中陈述文字和内容未经(企业库qiyeku.com)证实,请读者仅作参考,并请自行核实相关内容。若本文有侵犯到您的版权, 请你提供相关证明及申请并与我们联系(qiyeku # qq.com)或【在线投诉】,我们审核后将会尽快处理。
—— 相关资讯 ——