NO IMAGE

通過ContentProvider訪問手機裝置中聯絡人的資訊,並通過ListView展示出來。

一、設計main_activity.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.think.contentprovidertest.MainActivity">
<ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@ id/list_view"
        />
</LinearLayout>

二、設計ListView子項佈局item_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
<TextView
        android:id="@ id/text_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="15sp"
        />
<TextView
        android:id="@ id/text_phnumber"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="15sp"
        />
</LinearLayout>

三、編寫儲存聯絡人資訊的Data類:

package com.example.think.contentprovidertest;
/**
 * Created by Think on 2018/3/23.
 */

public class Data {
String name;
String phnumber;
public Data(String name,String phnumber) {
this.name=name;
this.phnumber=phnumber;
}
public String getName() {
return name;
}
public String getPhnumber() {
return phnumber;
}
}

四、定義ArrayAdapter子類(為ListView配置介面卡做準備):

package com.example.think.contentprovidertest;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.List;
/**
 * Created by Think on 2018/3/23.
 */

public class MyAdapter extends ArrayAdapter<Data> {
int mresource;
public MyAdapter(@NonNull Context context, int resource, @NonNull List<Data> objects) {
super(context, resource, objects);
mresource=resource;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
Data data=getItem(position);
View view;
ViewHolder holder;
if(convertView==null) {
view= LayoutInflater.from(getContext()).inflate(mresource,parent,false);
holder=new ViewHolder(view);
view.setTag(holder);
} else {
view=convertView;
holder=(ViewHolder)view.getTag();
}
holder.textView1.setText(data.getName());
holder.textView2.setText(data.getPhnumber());
return view;
}
class ViewHolder {
TextView textView1;
TextView textView2;
public ViewHolder(View view) {
textView1=(TextView)view.findViewById(R.id.text_name);
textView2=(TextView)view.findViewById(R.id.text_phnumber);
}
}
}

五、MianActivity:

package com.example.think.contentprovidertest;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
MyAdapter adapter;
ListView listView;
List<Data> dataList=new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView=(ListView)findViewById(R.id.list_view);
adapter=new MyAdapter(MainActivity.this,R.layout.item_layout,dataList);
listView.setAdapter(adapter);
initDatas();
}
public void initDatas() {
Cursor cursor=getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,null,null,null);
if(cursor!=null) {
while(cursor.moveToNext()) {
String name=cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phnumber=cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Data data=new Data(name,phnumber);
dataList.add(data);
}
cursor.close();
}
adapter.notifyDataSetChanged();
}
}

總結:這個程式雖然簡單,但它能夠很好的展示了對已有ContentProvider的使用,以及對ListView的使用,它的設計步驟主要如下:

1、編寫Adapter的子類,併為ListView繫結介面卡
2、編寫儲存聯絡人資訊的Data類
3、使用ContentResolver介面訪問手機裝置的聯絡人資訊
4、呼叫ContentResolver類中的query方法獲取聯絡人資訊,並將資訊新增到Data型別的連結串列dataList中
5、通過呼叫adapter物件的notifyDataSetChanged()方法將dataList中的資料更新到ListView中進行展現