android – 联系人列表视图

android – 联系人列表视图,第1张

概述我想要一个我的联系人ListView. 我使用Google示例代码. 问题是我一遍又一遍地得到相同的联系人: >吉姆 >吉姆 >吉姆 >吉姆 >吉姆 >安娜 >安娜 >安娜 >安娜 > …… 如何获得我的联系人的DISTINCT列表? public class ContactsListView extends ListActivityimplements LoaderManager.Loader 我想要一个我的联系人ListVIEw.
我使用Google示例代码.
问题是我一遍又一遍地得到相同的联系人:

>吉姆
>吉姆
>吉姆
>吉姆
>吉姆
>安娜
>安娜
>安娜
>安娜
> ……

如何获得我的联系人的disTINCT列表?

public class ContactsListVIEw extends ListActivityimplements LoaderManager.LoaderCallbacks<Cursor> {SimpleCursorAdapter mAdapter;static final String[] PROJECTION = new String[] {ContactsContract.Data._ID,ContactsContract.Data.disPLAY_name};static final String SELECTION = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '"        + ("1") + "'";@OverrIDeprotected voID onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    // Create a progress bar to display while the List loads    Progressbar progressbar = new Progressbar(this);    progressbar.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,Gravity.CENTER));    progressbar.setIndeterminate(true);    getListVIEw().setEmptyVIEw(progressbar);    // Must add the progress bar to the root of the layout    VIEwGroup root = (VIEwGroup) findVIEwByID(androID.R.ID.content);    root.addVIEw(progressbar);    // For the cursor adapter,specify which columns go into which vIEws    String[] fromColumns = {ContactsContract.Data.disPLAY_name};    int[] toVIEws = {androID.R.ID.text1}; // The TextVIEw in simple_List_item_1    // Create an empty adapter we will use to display the loaded data.    // We pass null for the cursor,then update it in onLoadFinished()    mAdapter = new SimpleCursorAdapter(this,androID.R.layout.simple_List_item_1,null,fromColumns,toVIEws,0);    setlistadapter(mAdapter);    // Prepare the loader.  Either re-connect with an existing one,// or start a new one.    getLoaderManager().initLoader(0,this);}// Called when a new Loader needs to be createdpublic Loader<Cursor> onCreateLoader(int ID,Bundle args) {    // Now create and return a CursorLoader that will take care of    // creating a Cursor for the data being displayed.    return new CursorLoader(this,ContactsContract.Data.CONTENT_URI,PROJECTION,SELECTION,ContactsContract.Contacts.disPLAY_name            + " ColLATE LOCAliZED ASC");}// Called when a prevIoUsly created loader has finished loadingpublic 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);}// Called when a prevIoUsly created loader is reset,making the data unavailablepublic 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);}@OverrIDe public voID onListItemClick(ListVIEw l,VIEw v,int position,long ID) {   // String  itemValue = (String) l.getItemAtposition(position);}}
解决方法 在您使用String [] fromColumns = {ContactsContract.Data.disPLAY_name};的行中,而不是

ContactsContract.Data.disPLAY_name

尝试

ContactsContract.Contacts.disPLAY_name_PRIMARY

在documentation中,您可以读取数据是表示任何数字或电子邮件地址或其他内容的条目,而联系人是表示一个人的条目.

总结

以上是内存溢出为你收集整理的android – 联系人列表视图全部内容,希望文章能够帮你解决android – 联系人列表视图所遇到的程序开发问题。

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

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

原文地址:https://www.54852.com/web/1129435.html

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

发表评论

登录后才能评论

评论列表(0条)

    保存