微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

android – 联系人列表视图

我想要一个我的联系人ListView.
我使用Google示例代码.
问题是我一遍又一遍地得到相同的联系人:

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

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

public class ContactsListView extends ListActivity
implements 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") + "'";

@Override
protected 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 created
public 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 loading
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);
}

// Called when a prevIoUsly created loader is reset,making the data unavailable
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);
}

@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中,您可以读取数据是表示任何数字或电子邮件地址或其他内容的条目,而联系人是表示一个人的条目.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐