Android - Custom Car Kit TextView CursorAdaptor - Suggestion Behavior

I am trying to implement a custom AutoCompleteTextView to select a contact phone number from a list of offers displaying the contact name, type of phone number and phone number. I created a custom CursorAdapter that defines and sets my Layout and TextViews for each sentence and requests contacts based on the text entered by the user through runQueryOnBackgroundThread. I ran into a problem when sentences seem correct for the first two values ​​entered (for example, "ab" offers "abcd" and "abyz"), but not for anything other than this (for example, "abc" offers "abyz"). For the latter, when the "abyz" clause is selected, the values ​​for "abcd" are returned.

Code for the main activity:

final ContactInfo cont = new ContactInfo(ctx);
    Cursor contacts = cont.getContacts2(null);
    startManagingCursor(contacts);

    ContactsAutoCompleteCursorAdapter adapter = new ContactsAutoCompleteCursorAdapter(this, contacts);
    mPersonText.setAdapter(adapter);
    mPersonText.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
            long arg3) {
            Cursor cursor = (Cursor) arg0.getItemAtPosition(arg2);
            String number = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
            mPersonNum.setText(number);
        }
    });

, :

public Cursor getContacts2(String where)
{
    Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
    String[] projection = new String[] {
            ContactsContract.CommonDataKinds.Phone._ID,
            ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
            ContactsContract.CommonDataKinds.Phone.TYPE,
            ContactsContract.CommonDataKinds.Phone.NUMBER};

    Cursor people = ctx.getContentResolver().query(uri, projection, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " COLLATE LOCALIZED ASC");

    return people;
}

CursorAdapter:

public class ContactsAutoCompleteCursorAdapter extends CursorAdapter implements Filterable {

private TextView mName, mType, mNumber;
private ContentResolver mContent;

public ContactsAutoCompleteCursorAdapter(Context context, Cursor c) {
    super(context, c);
    mContent = context.getContentResolver();
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {

    final LayoutInflater mInflater = LayoutInflater.from(context);
    final View ret = mInflater.inflate(R.layout.contacts_auto_list, null);

    mName = (TextView) ret.findViewById(R.id.name);
    mType = (TextView) ret.findViewById(R.id.phonetype);
    mNumber = (TextView) ret.findViewById(R.id.phonenum);

    return ret;
}

@Override
public void bindView(View view, Context context, Cursor cursor) {

    int nameIdx = cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
    int typeIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE);
    int numberIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);

    String name = cursor.getString(nameIdx);
    int type = cursor.getInt(typeIdx);
    String number = cursor.getString(numberIdx);

    mName.setText(name);
    if (type == 1) {mType.setText("Home");}
    else if (type == 2) {mType.setText("Mobile");}
    else if (type == 3) {mType.setText("Work");}
    else {mType.setText("Other");}
    mNumber.setText(number);

}

@Override
public String convertToString(Cursor cursor) {
    int nameCol = cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
    String name = cursor.getString(nameCol);
    return name;
}

@Override
public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
    // this is how you query for suggestions
    // notice it is just a StringBuilder building the WHERE clause of a cursor which is the used to query for results
    if (getFilterQueryProvider() != null) { return getFilterQueryProvider().runQuery(constraint); }

    String[] projection = new String[] {
            ContactsContract.CommonDataKinds.Phone._ID,
            ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
            ContactsContract.CommonDataKinds.Phone.TYPE,
            ContactsContract.CommonDataKinds.Phone.NUMBER};

    return mContent.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, projection, 
            "UPPER(" + ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + ") LIKE '" + constraint.toString().toUpperCase() + "%'", null, 
            ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
}

}

, "ab" AutoCompleteTextView, "abcd" "abyz" , , "abc", "abyz" . "abyz" , "abcd". , , :

enter image description hereenter image description here

, , , , . Android, , . !

+5
2

, . newView bindView, , , , , ...

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {

    final LayoutInflater mInflater = LayoutInflater.from(context);
    final View ret = mInflater.inflate(R.layout.contacts_auto_list, null);

    return ret;
}

@Override
public void bindView(View view, Context context, Cursor cursor) {

    int nameIdx = cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
    int typeIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE);
    int numberIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);

    String name = cursor.getString(nameIdx);
    int type = cursor.getInt(typeIdx);
    String number = cursor.getString(numberIdx);

    mName = (TextView) view.findViewById(R.id.name);
    mType = (TextView) view.findViewById(R.id.phonetype);
    mNumber = (TextView) view.findViewById(R.id.phonenum);

    mName.setText(name);
    if (type == 1) {mType.setText("Home");}
    else if (type == 2) {mType.setText("Mobile");}
    else if (type == 3) {mType.setText("Work");}
    else {mType.setText("Other");}
    mNumber.setText(number);
}
+2

runQueryOnBackgroundThread ,

getContacts2

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.sms_send);

    Cursor contacts = null;

    mAdapter= new ContactsAutoCompleteCursorAdapter(this, contacts);
    mTxtPhoneNo = (AutoCompleteTextView) findViewById(R.id.mmWhoNo);
    mTxtPhoneNo.setAdapter(mAdapter);

    mTxtPhoneNo.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // TODO Auto-generated method stub
            Cursor cursor = (Cursor) arg0.getItemAtPosition(arg2);
            String number = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
            mTxtPhoneNo.setText(number);

        }
    });


}

public class ContactsAutoCompleteCursorAdapter extends CursorAdapter implements Filterable {

private TextView mName, mType, mNumber;
private ContentResolver mContent;

public ContactsAutoCompleteCursorAdapter(Context context, Cursor c) {
    super(context, c);
    mContent = context.getContentResolver();
}




@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {

    final LayoutInflater mInflater = LayoutInflater.from(context);
    final View ret = mInflater.inflate(R.layout.custcontview, null);

    return ret;
}

@Override
public void bindView(View view, Context context, Cursor cursor) {

    int nameIdx = cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
    int typeIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE);
    int numberIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);

    String name = cursor.getString(nameIdx);
    int type = cursor.getInt(typeIdx);
    String number = cursor.getString(numberIdx);

    mName = (TextView) view.findViewById(R.id.ccontName);
    mType = (TextView) view.findViewById(R.id.ccontType);
    mNumber = (TextView) view.findViewById(R.id.ccontNo);

    mName.setText(name);
    if (type == 1) {mType.setText("Home");}
    else if (type == 2) {mType.setText("Mobile");}
    else if (type == 3) {mType.setText("Work");}
    else {mType.setText("Other");}
    mNumber.setText(number);
}


@Override
public String convertToString(Cursor cursor) {
    int nameCol = cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
    String name = cursor.getString(nameCol);
    return name;
}




@Override
public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
    // this is how you query for suggestions
    // notice it is just a StringBuilder building the WHERE clause of a cursor which is the used to query for results



if (constraint==null)
    return null;

    if (getFilterQueryProvider() != null) { return getFilterQueryProvider().runQuery(constraint); }

    String[] projection = new String[] {
            ContactsContract.CommonDataKinds.Phone._ID,
            ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
            ContactsContract.CommonDataKinds.Phone.TYPE,
            ContactsContract.CommonDataKinds.Phone.NUMBER};

    return mContent.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, projection, 
            "UPPER(" + ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + ") LIKE '%" + constraint.toString().toUpperCase() + "%' or UPPER(" + ContactsContract.CommonDataKinds.Phone.NUMBER + ") LIKE '%" + constraint.toString().toUpperCase() + "%' ", null, 
            ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
}

}

0

All Articles