android – 与装载程序管理器读取联系人

android – 与装载程序管理器读取联系人,第1张

概述我试图利用LoaderManager和CursorLoader来读取联系人的电话号码.我能够阅读联系人的显示名称,但我不知道如何阅读电话号码.任何指导都将非常感谢. // These are the Contacts rows that we will retrieve.static final String[] CONTACTS_SUMMARY_PROJECTION = new String[ 我试图利用LoaderManager和CursorLoader来读取联系人的电话号码.我能够阅读联系人的显示名称,但我不知道如何阅读电话号码.任何指导都将非常感谢.

// These are the Contacts rows that we will retrIEve.static final String[] CONTACTS_SUMMARY_PROJECTION = new String[] {        Contacts._ID,Contacts.disPLAY_name,ContactsContract.CommonDataKinds.Phone.NUMBER };// This is the Adapter being used to display the List's data.SimpleCursorAdapter mAdapter;// If non-null,this is the current filter the user has provIDed.String mCurFilter;@OverrIDepublic voID onActivityCreated(Bundle savedInstanceState) {    super.onActivityCreated(savedInstanceState);    // Give some text to display if there is no data. In a real    // application this would come from a resource.    setEmptyText("No phone numbers");    // Create an empty adapter we will use to display the loaded data.    mAdapter = new SimpleCursorAdapter(getActivity(),androID.R.layout.simple_List_item_2,null,new String[] {                    Contacts.disPLAY_name,ContactsContract.CommonDataKinds.Phone.NUMBER },new int[] { androID.R.ID.text1,androID.R.ID.text2 },0);    setlistadapter(mAdapter);    // Start out with a progress indicator.    setListShown(false);    // Prepare the loader. Either re-connect with an existing one,// or start a new one.    getLoaderManager().initLoader(0,this);}@OverrIDepublic voID onListItemClick(ListVIEw l,VIEw v,int position,long ID) {    Log.i("FragmentComplexList","Item clicked: " + ID);}public Loader<Cursor> onCreateLoader(int ID,Bundle args) {    // This is called when a new Loader needs to be created. This    // sample only has one Loader,so we don't care about the ID.    // First,pick the base URI to use depending on whether we are    // currently filtering.    Uri baseUri;    if (mCurFilter != null) {        baseUri = Uri.withAppendedpath(Contacts.CONTENT_FILTER_URI,Uri.encode(mCurFilter));    } else {        baseUri = Contacts.CONTENT_URI;    }    // Now create and return a CursorLoader that will take care of    // creating a Cursor for the data being displayed.    String select = "((" + Contacts.disPLAY_name + " NOTNulL) AND ("            + Contacts.HAS_PHONE_NUMBER + "=1) AND ("            + Contacts.disPLAY_name + " != '' ))";    return new CursorLoader(getActivity(),baseUri,CONTACTS_SUMMARY_PROJECTION,select,Contacts.disPLAY_name + " ColLATE LOCAliZED ASC");}public voID onLoadFinished(Loader<Cursor> loader,Cursor data) {    // Swap the new cursor in. (The framework will take care of closing the    // old cursor once we return.)    mAdapter.swapCursor(data);    // The List should Now be shown.    if (isResumed()) {        setListShown(true);    } else {        setListShownNoAnimation(true);    }}public voID onLoaderreset(Loader<Cursor> loader) {    // This is called when the last Cursor provIDed to onLoadFinished()    // above is about to be closed. We need to make sure we are no    // longer using it.    mAdapter.swapCursor(null);}
解决方法 试试这个..这对我有用!

// This is the Adapter being used to display the List's data.    SimpleCursorAdapter mAdapter;    // If non-null,this is the current filter the user has provIDed.    String mCurFilter;    @OverrIDe public voID onActivityCreated(Bundle savedInstanceState) {        super.onActivityCreated(savedInstanceState);        // Give some text to display if there is no data.  In a real        // application this would come from a resource.        setEmptyText("No phone numbers");        // We have a menu item to show in action bar.        setHasOptionsMenu(true);        // Create an empty adapter we will use to display the loaded data.        mAdapter = new SimpleCursorAdapter(getActivity(),androID.R.layout.simple_List_item_1,new String[] { ContactsContract.CommonDataKinds.Phone.NUMBER,},new int[] { androID.R.ID.text1},0);        setlistadapter(mAdapter);        // Start out with a progress indicator.        setListShown(false);        // Prepare the loader.  Either re-connect with an existing one,// or start a new one.        getLoaderManager().initLoader(0,this);    }    @OverrIDe public voID onCreateOptionsMenu(Menu menu,MenuInflater inflater) {        // Place an action bar item for searching.        MenuItem item = menu.add("Search");        item.setIcon(androID.R.drawable.ic_menu_search);        item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);        VIEw searchVIEw = SearchVIEwCompat.newSearchVIEw(getActivity());        if (searchVIEw != null) {            SearchVIEwCompat.setonqueryTextListener(searchVIEw,new OnqueryTextListenerCompat() {                @OverrIDe                public boolean onqueryTextChange(String newText) {                    // Called when the action bar search text has changed.  Update                    // the search filter,and restart the loader to do a new query                    // with this filter.                    mCurFilter = !TextUtils.isEmpty(newText) ? newText : null;                    getLoaderManager().restartLoader(0,CursorLoaderListFragment.this);                    return true;                }            });            item.setActionVIEw(searchVIEw);        }    }    @OverrIDe public voID onListItemClick(ListVIEw l,long ID) {        // Insert desired behavior here.        Log.i("FragmentComplexList","Item clicked: " + ID);    }    // These are the Contacts rows that we will retrIEve.    static final String[] CONTACTS_SUMMARY_PROJECTION = new String[] {        ContactsContract.CommonDataKinds.Phone._ID,ContactsContract.CommonDataKinds.Phone.disPLAY_name,ContactsContract.CommonDataKinds.Phone.NUMBER,};    public Loader<Cursor> onCreateLoader(int ID,Bundle args) {        // This is called when a new Loader needs to be created.  This        // sample only has one Loader,so we don't care about the ID.        // First,pick the base URI to use depending on whether we are        // currently filtering.        Uri baseUri;        if (mCurFilter != null) {            baseUri = Uri.withAppendedpath(ContactsContract.CommonDataKinds.Phone.CONTENT_FILTER_URI,Uri.encode(mCurFilter));        } else {            baseUri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;        }        // Now create and return a CursorLoader that will take care of        // creating a Cursor for the data being displayed.        String select = "((" + ContactsContract.Contacts.disPLAY_name + " NOTNulL) AND ("                + ContactsContract.Contacts.disPLAY_name + " != '' )" + "AND ("+ContactsContract.Contacts.HAS_PHONE_NUMBER +" != '0'"+"))";        return new CursorLoader(getActivity(),null + " ColLATE LOCAliZED ASC");    }    public voID onLoadFinished(Loader<Cursor> loader,Cursor data) {        // Swap the new cursor in.  (The framework will take care of closing the        // old cursor once we return.)        mAdapter.swapCursor(data);        // The List should Now be shown.        if (isResumed()) {            setListShown(true);        } else {            setListShownNoAnimation(true);        }    }    public voID onLoaderreset(Loader<Cursor> loader) {        // This is called when the last Cursor provIDed to onLoadFinished()        // above is about to be closed.  We need to make sure we are no        // longer using it.        mAdapter.swapCursor(null);    }}
总结

以上是内存溢出为你收集整理的android – 与装载程序管理器读取联系人全部内容,希望文章能够帮你解决android – 与装载程序管理器读取联系人所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/web/1126686.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存