首页 > 代码库 > Apidemos-->Views-Lists-Cursor(people)学?

Apidemos-->Views-Lists-Cursor(people)学?

Apidemos-->Views-Lists-Cursor(people)-主要用到了获取手机联系人信息,属于内容提供者的范畴,要想了解这方面的内容,能够參考官方docs
/sdk/docs/guide/topics/providers/content-provider-basics.html

这里简单说下contentprovider;
contentprovider可实现不同应用程序之间的数据共享. 它是程序之间进行数据交换的api. 
当一个程序须要把自己的数据暴露给其它程序使用时,该应用程序就能够通过提供contentprovider来实现,其它应用程序也能够通过来contentResolver操作contentprovider暴露的数据.

实例中用到了系统提供的联系人contentprovider:
经常使用的几个Uri有:
ContactsContract.Contacts.CONTENT_URL   //管理联系人的URI
ContactsContract.Contacts._ID//联系人Id
ContactsContract.Contacts.DISPLAY_NAME //联系人name 的Uri
ContactsContract.CommonDataKinds.Phone.CONTENT_URI 管理联系的电话的URI
ContactsContract.CommonDataKinds.Email.CONTENT_URI 管理联系人的E-mail的URI

随便写下自己犯了一个超级低级的错误就是. 
竟然採用for来遍历cursor,把 moveToNext丢了.  还有使用完关闭游标,加上mCursor.close(); 
package com.example.testmyviewslistscursorpeople;

import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.content.Context;
import android.database.Cursor;
import android.net.LocalSocketAddress.Namespace;
import android.os.Bundle;
import android.provider.ContactsContract.Contacts;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.text.GetChars;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.example.testmyviewslistsactivateitems.R;

/**
 * 
 * @author Administrator 仿效果slow loading apiDemos -- Views -Lists - Slow Adapter
 */
public class Main extends ListActivity {

	private boolean mBusy = false;
	public static List<String> mStrings = new ArrayList<String>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getPeople();
		setListAdapter(new SlowAdapter(this));
		// 设置选择模式为单选
		getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
		// 首次载入设置选中items
		getListView().setItemChecked(0, true);
		getListView().setOnScrollListener(new OnScrollListener());

	}

	protected class OnScrollListener implements ListView.OnScrollListener {

		@Override
		public void onScrollStateChanged(AbsListView view, int scrollState) {
			switch (scrollState) {
			// The view is not scrolling.
			case OnScrollListener.SCROLL_STATE_IDLE:
				mBusy = false;
				int first = view.getFirstVisiblePosition();
				int count = view.getChildCount();
				for (int i = 0; i < count; i++) {
					TextView t = (TextView) view.getChildAt(i);
					if (t.getTag() != null) {
						t.setText(mStrings.get(first + i));
						t.setTag(null);
					}
				}
				break;
			// The user is scrolling using touch, and their finger is still on
			// the screen
			case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
				mBusy = true;
				break;
			// The user had previously been scrolling using touch and had
			// performed a fling.
			// The animation is now coasting to a stop
			case OnScrollListener.SCROLL_STATE_FLING:
				mBusy = true;
				break;
			}
		}

		@Override
		public void onScroll(AbsListView view, int firstVisibleItem,
				int visibleItemCount, int totalItemCount) {
		}

	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		getListView().setItemChecked(position, true);
	}

	// 自己定义适配器
	private class SlowAdapter extends BaseAdapter {
		private LayoutInflater mInflater;

		public SlowAdapter(Context context) {
			mInflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}

		@Override
		public int getCount() {
			return mStrings.size();
		}

		@Override
		public Object getItem(int position) {
			return position;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			TextView text;

			if (convertView == null) {
				text = (TextView) mInflater.inflate(R.layout.main, null, false);
			} else {
				text = (TextView) convertView;
			}

			if (!mBusy) {
				text.setText(mStrings.get(position).toString());
				text.setTag(null);
			} else {
				text.setText("Loading...");
				text.setTag(this);
			}
			return text;
		}

	}

	protected  void  getPeople() {
		final String[] CONTACT_PROJECTION = new String[] { Contacts._ID,
				Contacts.DISPLAY_NAME, };
		Cursor mCursor = getContentResolver().query(Contacts.CONTENT_URI,
				CONTACT_PROJECTION, null, null, null); 
		 while(mCursor.moveToNext()) {
		   String name =
		   mCursor.getString(mCursor.getColumnIndex(Contacts.DISPLAY_NAME)); 
		   mStrings.add(name);
		 } 
                mCursor.close();
	} 
}

实例源代码

添加?获取手机联系人电话的方法(仅仅为了解获取方法,尚未和联系人一一相应,兴许将改进):
	protected void getPeople() {
		final String[] CONTACT_PROJECTION = new String[] { Contacts._ID,
				Contacts.DISPLAY_NAME };
		//联系人
		Cursor mCursor = getContentResolver().query(Contacts.CONTENT_URI,
				CONTACT_PROJECTION, null, null, null);
		//联系人电话
		Cursor phones = getContentResolver().query(
				ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
				ContactsContract.CommonDataKinds.Phone.CONTACT_ID, null, null);
		while (mCursor.moveToNext()) {
			// 获取联系人
			String name = mCursor.getString(mCursor
					.getColumnIndex(Contacts.DISPLAY_NAME));
			mStrings.add(name);
			if (phones.moveToNext()) {
				//获取电话
				String phone = phones.getString(phones
						.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
				mStrings.add(phone);
			}
		
		}
		//关闭资源
		mCursor.close();
		phones.close();
	}

Apidemos--&gt;Views-Lists-Cursor(people)学?