Skip to content

allowDuplicates(false) not working with SimpleCursorAdapter #39

Description

@jjalonso

I think that i find a little bug.
allowDuplicates(false) appear dont work when is used with a SimpleCursorAdapter.

For reproduce it, this is my code.

Custom TokenCompleteTextView

public class ContactCompleteTextView extends TokenCompleteTextView {

    public ContactCompleteTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        allowDuplicates(false);
        setTokenClickStyle(TokenCompleteTextView.TokenClickStyle.Delete);
    }

    @Override
    protected View getViewForObject(Object object) {
        if (object != null) {
            Cursor c = (Cursor) object;

            LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(
                    Context.LAYOUT_INFLATER_SERVICE);
            LinearLayout view = (LinearLayout) inflater.inflate(R.layout.contact_token,
                    (ViewGroup) ContactCompleteTextView.this.getParent(), false);

            String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            ((TextView) view.findViewById(R.id.name)).setText(name);
            return view;
        }
        return null;
    }

    @Override
    protected Object defaultObject(String completionText) {
        // Avoid creation
        return null;
      }
  }

Initialization of adapter and view

        final String contactIdCol = ContactsContract.CommonDataKinds.Phone._ID;
        final String contactNameCol = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME;
        final String contactPhoneCol = ContactsContract.CommonDataKinds.Phone.NUMBER;

        int[] displayViews = new int[] { android.R.id.text1, android.R.id.text2 };
        String[] displayCols = new String[] {
                contactNameCol,
                contactPhoneCol
        };

        final String[] queryProjection = new String[] {
                contactIdCol,
                contactNameCol,
                contactPhoneCol
        };

        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
                android.R.layout.simple_list_item_2, null, displayCols, displayViews, 0);

        adapter.setFilterQueryProvider(new FilterQueryProvider() {
            public Cursor runQuery(CharSequence constraint) {
                return getContentResolver().query(
                        ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                        queryProjection,
                        contactNameCol + " LIKE '%" + constraint + "%'",
                        null, null);
            }
        });

        mTtvContacts.setAdapter(adapter);

Activity

  1. mgod commented on Mar 14, 2014

    @mgod
    Contributor

    allowDuplicates(false) depends on the objects equals method. Can you check to see if two cursors that you think should be the same return true with equals?

  2. jjalonso commented on Mar 14, 2014

    @jjalonso
    Author

    Is different because apply other filter query.

    Juan Jose Alonso
    http://jjalon.so
    El 14/03/2014 01:41, "mgod" [email protected] escribió:

    allowDuplicates(false) depends on the objects equals method. Can you
    check to see if two cursors that you think should be the same return true
    with equals?

    —
    Reply to this email directly or view it on GitHubhttps://github.com//issues/39#issuecomment-37604361
    .

  3. mgod commented on Mar 9, 2019

    @mgod
    Contributor

    Version 3.0.0 now allows you to override the comparison for duplicates in shouldIgnoreToken

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions