ContentProvider的使用

关于ContentProvider就不再过多介绍啦~~在这里给出个实例,现在关于安卓的书籍很多,垃圾也很多~~上面写的代码逻辑严重有误,考虑不周全~~对这些书的作者严重鄙视!!!

 

最近看你了一本Android 从入门到精通的书,上面代码逻辑不严谨,很多地方需要修改,在这里给出个实例,是对上面一个例子的补充~~

 

获取联系人

 

 

package com.example.contacts;

import java.util.ArrayList;

import android.os.Bundle;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.Contacts;
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.graphics.Color;
import android.view.Menu;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity {

	private ListView lv;
	private TextView tv;
	private ArrayList<String> list = new ArrayList<String>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		getContacts();
		setAdapter();
	}

	public void getContacts() {//获取联系人

		String s[] = { Contacts._ID, Contacts.DISPLAY_NAME, Phone.NUMBER,
				Phone._ID,Phone.DISPLAY_NAME };

		ContentResolver cr = getContentResolver();

		Cursor cs = cr.query(Contacts.CONTENT_URI, null, null, null, null);///获取所有联系人

		while (cs.moveToNext()) {
			StringBuilder ss = new StringBuilder();/记录联系人和电话
			int idindex = cs.getColumnIndex(s[0]);
			int id = cs.getInt(idindex);
			int nameid = cs.getColumnIndex(s[1]);
			String name = cs.getString(nameid);
			ss.append(name);
			Cursor phone = cr.query(Phone.CONTENT_URI, null, null,null, null);/获取所有电话号码
		/*
		 * 以下查找不可取,一个联系人不可能只存一个电话,所以contacts表中的id和phone表中的number并不是
		 * 一一对应的,是一对多的关系,所以必须按照姓名一一遍历查找
		 * 
		 * 
		 * 	Cursor phone = cr.query(Phone.CONTENT_URI, null, s[3]+"="+id,
					null, null);
					
					*
					*
					*/
			while (phone.moveToNext()) {

				int numberindex = phone.getColumnIndex(s[2]);
				String number = phone.getString(numberindex);
				int nameindex1=phone.getColumnIndex(s[4]);
				String name1=phone.getString(nameindex1);
				if(name1.equals(name)){contacts表中的name和phone表中的name相同的取出号码
				ss.append(":" + number);
				}
			}

			list.add(ss.toString());
			ss = null;

		}

	}

	public void setAdapter(){//设置listview
		
		lv=(ListView)findViewById(R.id.lv);
		
		 ArrayAdapter<String> aa=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,list);
		lv.setAdapter(aa);
		
		
	} 
	
}


 

 

清单文件

 

 

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.contacts"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.contacts.MainActivity"
            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>

</manifest>


 

 

运行截图:

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值