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

來源:互聯網
上載者:User

標籤:android   style   http   io   ar   os   使用   java   sp   

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   //管理連絡人的URIContactsContract.Contacts._ID//連絡人Id
ContactsContract.Contacts.DISPLAY_NAME //連絡人name 的Uri
ContactsContract.CommonDataKinds.Phone.CONTENT_URI 管理聯絡的電話的URIContactsContract.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>();@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);getPeople();setListAdapter(new SlowAdapter(this));// 設定選擇模式為單選getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);// 首次載入設定選中itemsgetListView().setItemChecked(0, true);getListView().setOnScrollListener(new OnScrollListener());}protected class OnScrollListener implements ListView.OnScrollListener {@Overridepublic 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 screencase 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 stopcase OnScrollListener.SCROLL_STATE_FLING:mBusy = true;break;}}@Overridepublic void onScroll(AbsListView view, int firstVisibleItem,int visibleItemCount, int totalItemCount) {}}@Overrideprotected 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);}@Overridepublic int getCount() {return mStrings.size();}@Overridepublic Object getItem(int position) {return position;}@Overridepublic long getItemId(int position) {return position;}@Overridepublic 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)學?

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.