Android的 - 自动完成与联系人自动完成、联系人、Android

2023-09-13 00:34:54 作者:渡口有过寒鸦

我已经创建了一个显示所有联系人的姓名的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联系人中选择。低于code:

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.

希望这有助于。