Android - 使用联系人自动完成 [英] Android - Autocomplete with contacts

查看:19
本文介绍了Android - 使用联系人自动完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个 AutoCompleteTextView 框来显示所有联系人的姓名,但在查看 Android API 后,我的方法似乎效率很低.

I've created an AutoCompleteTextView box that displays the names of all contacts, but after looking in the Android APIs, it seems my method is probably quite inefficient.

目前我正在抓取所有联系人的光标,将每个姓名和每个联系人 ID 放入两个不同的数组中,然后将名称数组传递给 AutoCompleteTextView.当用户选择一个项目时,我会在上面创建的第二个 id 数组中查找联系人选择的 ID.代码如下:

Currently I am grabbing a cursor of the all the contacts, placing each name and each contact id into two different arrays, then passing the name array to the AutoCompleteTextView. When a user selects an item, I lookup which ID the contact selected in the second id array created above. Code below:

private ContactNames mContactData;

// Fill the autocomplete textbox
Cursor contactsCursor = grabContacts();
mContactData = new ContactNames(contactsCursor);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.contact_name, mContactData.namesArray);
mNameText.setAdapter(adapter);

private class ContactNames {
    private String[] namesArray;
    private long[] idsArray;        

    private ContactNames(Cursor cur) {
        namesArray = new String[cur.getCount()];
        idsArray = new long[cur.getCount()];

        String name; 
        Long contactid;
        // Get column id's
        int nameColumn = cur.getColumnIndex(People.NAME); 
        int idColumn = cur.getColumnIndex(People._ID);
        int i=0;
        cur.moveToFirst();
        // Check that there are actually any contacts returned by the cursor
        if (cur.getCount()>0){
            do {
                // Get the field values
                name = cur.getString(nameColumn);
                contactid = Long.parseLong(cur.getString(idColumn));
                // Do something with the values. 
                namesArray[i] = name;
                idsArray[i] = contactid;
                i++;
            } while (cur.moveToNext());
        }
    }

    private long search(String name){
        // Lookup name in the contact list that we've put in an array
        int indexOfName = Arrays.binarySearch(namesArray, name);
        long contact = 0;
        if (indexOfName>=0)
        {
            contact = idsArray[indexOfName];
        }
        return contact;
    }
}

private Cursor grabContacts(){
    // Form an array specifying which columns to return. 
    String[] projection = new String[] {People._ID, People.NAME};

    // Get the base URI for the People table in the Contacts content provider.
    Uri contacts =  People.CONTENT_URI;

    // Make the query. 
    Cursor managedCursor = managedQuery(contacts, projection, null, null, People.NAME + " ASC"); // Put the results in ascending order by name
    startManagingCursor(managedCursor);
    return managedCursor;
}

必须有更好的方法来做到这一点 - 基本上我正在努力了解如何找到用户在 AutoCompleteTextView 中选择的项目.有什么想法吗?

There must be a better way of doing this - basically I'm struggling to see how I can find which item a user selected in an AutoCompleteTextView. Any ideas?

干杯.

推荐答案

构建您自己的光标适配器并将其用于 AutoCompleteTextView.

Build your own cursor adapter and use it for AutoCompleteTextView.

CursorAdapter 中有一个 convertToString() 函数,您需要覆盖它才能获取要在 TextView 中显示的详细信息.它将为您提供指向所选位置的光标作为参数.

There is a convertToString() function in CursorAdapter that you need to overwrite to get the details that you want to be displayed in the TextView. It will give you the cursor pointing to the selected position as a parameter.

希望这会有所帮助.

这篇关于Android - 使用联系人自动完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆