【问题标题】:ListView of Contacts联系人列表视图
【发布时间】:2014-06-09 09:03:11
【问题描述】:

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

  • 吉姆
  • 吉姆
  • 吉姆
  • 吉姆
  • 吉姆
  • 安娜
  • 安娜
  • 安娜
  • 安娜
  • ...

如何获取我的联系人的 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, null, 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, null, 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);
}
}

【问题讨论】:

    标签: android android-contacts


    【解决方案1】:

    在你的行中使用String[] fromColumns = {ContactsContract.Data.DISPLAY_NAME};,而不是

    ContactsContract.Data.DISPLAY_NAME
    

    试试

    ContactsContract.Contacts.DISPLAY_NAME_PRIMARY
    

    documentation 中,您可以看到Data 是代表任何数字或电子邮件地址或其他任何内容的条目,而Contacts 是代表一个人的条目。

    【讨论】:

    • 我还将 CursorLoad uri 更改为:return new CursorLoader(this, ContactsContract.Contacts.CONTENT_URI, PROJECTION, SELECTION, null, ContactsContract.Contacts.DISPLAY_NAME_PRIMARY + "COLLATE LOCALIZED ASC");
    【解决方案2】:

    试试这个代码

    String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '"
                    + ("1") + "'";
            String sortOrder = ContactsContract.Contacts.DISPLAY_NAME
                    + " COLLATE LOCALIZED ASC";
    cur = context.getContentResolver().query(
                    ContactsContract.Contacts.CONTENT_URI, projection, selection
                            + " AND " + ContactsContract.Contacts.HAS_PHONE_NUMBER
                            + "=1", null, sortOrder);// this query only return contacts which had phone number and not duplicated
    

    【讨论】:

    • 我应该在哪里添加对 context.getContentResolver().query 的调用?
    【解决方案3】:

    为您的代码))

    // 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
                            + " AND " + ContactsContract.Contacts.HAS_PHONE_NUMBER
                            + "=1", null, sortOrder);
    }
    

    【讨论】:

    • 我有同样的行为
    【解决方案4】:

    使用集合而不是列表。 Set 仅包含唯一元素,因此结果集合中不存在重复元素。

    【讨论】:

    • 听起来不错。所以我该怎么做?我根本不使用 List 类?
    猜你喜欢
    • 1970-01-01
    • 2012-02-08
    • 1970-01-01
    • 2023-03-22
    • 2016-09-24
    • 1970-01-01
    • 2014-03-21
    • 2018-04-10
    相关资源
    最近更新 更多