Contacts & Autocomplete

Posted by Vince on Stack Overflow See other posts from Stack Overflow or by Vince
Published on 2012-12-10T05:02:04Z Indexed on 2012/12/10 5:03 UTC
Read the original article Hit count: 197

Filed under:
|

First post. I'm new to android and programming in general. What I'm attempting to is to have an autocomplete text box pop up with auto complete names from the contact list. IE, if they type in "john" it will say "John Smith" or any john in their contacts. The code is basic, I pulled it from a few tutorials.

private void autoCompleteBox() {

    ContentResolver cr = getContentResolver();

    Uri contacts = Uri.parse("content://contacts/people");

    Cursor managedCursor1 = cr.query(contacts, null, null, null, null);

    if (managedCursor1.moveToFirst()) {

        String contactname;
        String cphoneNumber;

        int nameColumn = managedCursor1.getColumnIndex("name");
        int phoneColumn = managedCursor1.getColumnIndex("number");

        Log.d("int Name", Integer.toString(nameColumn));
        Log.d("int Number", Integer.toString(phoneColumn));

        do {
            // Get the field values
            contactname = managedCursor1.getString(nameColumn);
            cphoneNumber = managedCursor1.getString(phoneColumn);
            if ((contactname != " " || contactname != null)
                    && (cphoneNumber != " " || cphoneNumber != null)) {

                c_Name.add(contactname);
                c_Number.add(cphoneNumber);

                Toast.makeText(this, contactname, Toast.LENGTH_SHORT)
                .show();
            }

        } while (managedCursor1.moveToNext());

    }

    name_Val = (String[]) c_Name.toArray(new String[c_Name.size()]);
    phone_Val = (String[]) c_Number.toArray(new String[c_Name.size()]);

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_dropdown_item_1line, name_Val);

    personName.setAdapter(adapter);

}

personName is my autocompletetextbox. So it actually works when I use it in the emulator (4.2) with manually entered contacts through the people app, but when I use it on my device, it will not pop up with any names. I'm sure it's something ridiculous but I've tried to find the answer and I'm getting nowhere. Can't learn if I don't ask.

© Stack Overflow or respective owner

Related posts about java

Related posts about android