最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android使用ContentResolver搜索手機(jī)通訊錄的方法

 更新時(shí)間:2016年01月29日 15:02:27   作者:馬到成功  
這篇文章主要介紹了Android使用ContentResolver搜索手機(jī)通訊錄的方法,結(jié)合實(shí)例形式分析了Android中ContentResolver操作手機(jī)通訊錄的具體步驟與相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下

本文實(shí)例講述了Android使用ContentResolver搜索手機(jī)通訊錄的方法。分享給大家供大家參考,具體如下:

在這個(gè)程序中使用ContentResolver來訪問通訊錄里聯(lián)系人的關(guān)鍵字,并將所有找到的聯(lián)系人存入CursorAdapter里。輸入搜索人員名字a ,會(huì)將所有以a開頭的名字都顯示出來,輸入*,則所有通訊錄中的人名顯示于AutoCompleteView的AdapterView里,若發(fā)生了User選擇事件后,會(huì)將勾選的聯(lián)系人電話號(hào)碼顯示于TextView里。

此程序中用到了ContentResolver .query(Uri  uri, String[]  projection, String  selection, String[]    selectionArgs, String  sortOrder)來取出通訊錄里的所有聯(lián)系人;其中將selection及selectionArgs傳入null代表將所有聯(lián)系人找出來。用Cursor 的getString(column index)的方式來取得存儲(chǔ)內(nèi)容,其中column index從0開始,這一點(diǎn)與java.sql.ResultSet不同,因?yàn)樗菑?開始的。用AutoCompleteTextView.OnItemClickListener事件,這也是當(dāng)用戶單擊聯(lián)系人之后所攔截的事件處理,在其中便以ContactsAdapter.getCursor()方法取得聯(lián)系人的電話號(hào)碼。

程序如下:

import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.Contacts;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AutoCompleteTextView;
import android.widget.TextView;
@SuppressWarnings("deprecation")
public class A07Activity extends Activity {
 private AutoCompleteTextView actv;
 private TextView tv;
 private Cursor c01;
 private ContactsAdapter ca;
 //找出通訊錄中的字段
 public static String[] people={
  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);
    actv=(AutoCompleteTextView)findViewById(R.id.actv);
    tv=(TextView)findViewById(R.id.tv);
    ContentResolver cr=getContentResolver();
    c01=cr.query(Contacts.People.CONTENT_URI, people, null, null, Contacts.People.DEFAULT_SORT_ORDER);
    ca=new ContactsAdapter(this,c01);
    actv.setAdapter(ca);
    actv.setOnItemClickListener(new OnItemClickListener(){
  @Override
  public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
   long arg3) {
  // TODO Auto-generated method stub
  Cursor c02=ca.getCursor();
  c02.moveToPosition(arg2);
  String number=c02.getString(c02.getColumnIndexOrThrow(Contacts.People.NUMBER));
  number=number==null?"無電話輸入": number;
  tv.setText(c02.getString(c02.getColumnIndexOrThrow(Contacts.People.NAME))+"的電話是:"+number);
  }
    });
  }
}

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.provider.Contacts;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.TextView;
@SuppressWarnings("deprecation")
public class ContactsAdapter extends CursorAdapter{
 ContentResolver cr;
 public ContactsAdapter(Context context, Cursor c) {
 super(context, c);
 // TODO Auto-generated constructor stub
 cr=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 li=LayoutInflater.from(context);
 final TextView tv=(TextView)li.inflate(android.R.layout.simple_dropdown_item_1line, parent, false);
 tv.setText(cursor.getString(cursor.getColumnIndexOrThrow(Contacts.People.NAME)));
 return tv;
 }
 public String converToString(Cursor c){
 return c.getString(c.getColumnIndexOrThrow(Contacts.People.NAME));
 }
 @SuppressWarnings("null")
 public Cursor runQueryOnBackgroundThread(CharSequence cs){
 if(getFilterQueryProvider()!=null){
  getFilterQueryProvider().runQuery(cs);
 }
 StringBuilder sb=null;
 String[] s=null;
 if(cs==null){
  sb=new StringBuilder();
  sb.append("UPPER(");
  sb.append(Contacts.ContactMethods.NAME);
  sb.append(")GLOB?");
   s=new String[]{cs.toString().toUpperCase()+"*"};
 }
 return cr.query(
    Contacts.People.CONTENT_URI,
    A07Activity.people, 
    sb==null? null:sb.toString(),
     s, 
     Contacts.People.DEFAULT_SORT_ORDER
     );
 }
}

AndroidManifest.xml如下所示:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.my.a07"
  android:versionCode="1"
  android:versionName="1.0" >
  <uses-sdk android:minSdkVersion="10" />
  <application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
      android:name=".A07Activity"
      android:label="@string/app_name" >
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
    </activity>
  </application>
  <uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>
</manifest>

通過ContentResolver還可以添加、修改、刪除通訊錄中的信息;并且通過它還可以訪問audio、video、images等數(shù)據(jù)。相應(yīng)方法展示如下:

添加:public final Uri insert(Uri uri,ContentValues values),ContentValue.put(key,value) ,其中key為字段名稱,value為添加的數(shù)據(jù)。

修改:public final int update(Uri uri,ContentValues values,String where ,String[] selectionArgs),其中where為sql where 后面的條件字符串。selectionArgs為where里的數(shù)據(jù)。

刪除:public final int delete(Uri uri,String where,String[] selectionArgs).

下面再使用ContentValue.put()方法。通過程序添加通訊錄里的聯(lián)系人的資料。

更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android控件用法總結(jié)》及《Android開發(fā)入門與進(jìn)階教程

希望本文所述對大家Android程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • Android如何防止多次點(diǎn)擊事件

    Android如何防止多次點(diǎn)擊事件

    大家都遇到這樣一個(gè)問題吧,一個(gè)點(diǎn)擊事件多次觸發(fā)導(dǎo)致同樣的內(nèi)容提交了很多次,下面小編給大家?guī)硪黄恼聨椭蠹医鉀Qandroid如何防止多次點(diǎn)擊事件,對android防止多次點(diǎn)擊事件相關(guān)只是感興趣的朋友一起學(xué)習(xí)吧
    2015-12-12
  • Android自定義view之圍棋動(dòng)畫效果的實(shí)現(xiàn)

    Android自定義view之圍棋動(dòng)畫效果的實(shí)現(xiàn)

    這篇文章主要介紹了Android自定義view之圍棋動(dòng)畫效果的實(shí)現(xiàn),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-12-12
  • android okhttp的基礎(chǔ)使用【入門推薦】

    android okhttp的基礎(chǔ)使用【入門推薦】

    本文主要總結(jié)了Android著名網(wǎng)絡(luò)框架-okhttp的基礎(chǔ)使用。具有一定的參考價(jià)值,下面跟著小編一起來看下吧
    2017-01-01
  • 簡單實(shí)現(xiàn)Android端搜索框示例詳解

    簡單實(shí)現(xiàn)Android端搜索框示例詳解

    這篇文章主要為大家介紹了簡單實(shí)現(xiàn)Android端搜索框示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-11-11
  • Android自定義折線圖控件的完整步驟

    Android自定義折線圖控件的完整步驟

    折線圖是常用的圖表之一,最近的工作中又遇到了相關(guān)的需求,所以下面這篇文章主要給大家介紹了關(guān)于Android自定義折線圖控件的完整步驟,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-06-06
  • Android 8.0版本更新無法自動(dòng)安裝問題的解決方法

    Android 8.0版本更新無法自動(dòng)安裝問題的解決方法

    這篇文章主要為大家詳細(xì)介紹了Android 8.0版本更新無法自動(dòng)安裝問題的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-06-06
  • android開機(jī)自啟動(dòng)app示例分享

    android開機(jī)自啟動(dòng)app示例分享

    這篇文章主要介紹了android開機(jī)自動(dòng)啟動(dòng)APP的方法,大家參考使用吧
    2014-01-01
  • Android實(shí)現(xiàn)長按圓環(huán)動(dòng)畫View效果的思路代碼

    Android實(shí)現(xiàn)長按圓環(huán)動(dòng)畫View效果的思路代碼

    這篇文章主要介紹了Android實(shí)現(xiàn)長按圓環(huán)動(dòng)畫View效果,本文給大家分享實(shí)現(xiàn)思路,通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-09-09
  • Android中PopupMenu組件的使用實(shí)例

    Android中PopupMenu組件的使用實(shí)例

    本篇文章主要介紹了Android中PopupMenu組件的使用實(shí)例,詳細(xì)的介紹了PopupMenu組件的使用,具體一定的參考價(jià)值,有興趣的可以了解一下
    2017-07-07
  • Android中NavigationView的使用與相關(guān)問題解決

    Android中NavigationView的使用與相關(guān)問題解決

    大家都知道NavigationView的引入讓 Android側(cè)邊欄實(shí)現(xiàn)起來相當(dāng)方便,最近公司項(xiàng)目中也使用這個(gè)新的控件完成了側(cè)邊欄的改版。在使用過程中遇到一些問題所以記錄一下。本文分為兩個(gè)部分,一是基本使用,二是相關(guān)問題的解決,感興趣的朋友們下面來一起看看吧。
    2016-10-10

最新評(píng)論

绍兴市| 郸城县| 尚义县| 门源| 泰州市| 宝丰县| 曲水县| 新巴尔虎右旗| 泰和县| 荥阳市| 长垣县| 冷水江市| 柯坪县| 丹凤县| 色达县| 湖州市| 大港区| 札达县| 平舆县| 天祝| 陕西省| 新乡市| 霍城县| 乐陵市| 收藏| 江陵县| 临洮县| 长汀县| 旬阳县| 巨野县| 育儿| 三明市| 墨江| 梅州市| 高碑店市| 曲靖市| 霍邱县| 元阳县| 多伦县| 桓台县| 日土县|