【问题标题】:Android - Autocomplete with contactsAndroid - 使用联系人自动完成
【发布时间】:2011-02-07 09:26:28
【问题描述】:

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

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

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 中选择的项目。有什么想法吗?

干杯。

【问题讨论】:

    标签: android autocomplete


    【解决方案1】:
    【解决方案2】:

    如果您仍然拥有该类的实例,您应该能够搜索您在适配器中使用的数组来收集您需要的信息。

    【讨论】:

      【解决方案3】:

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

      CursorAdapter 中有一个 convertToString() 函数,您需要覆盖该函数以获取您希望在 TextView 中显示的详细信息。它会给你指向所选位置的光标作为参数。

      希望这会有所帮助。

      【讨论】:

      • 干杯,我想我的问题更多的是一旦选择了一个名字,有没有告诉它是否在自动完成列表中,或者我是否必须重新查询联系人数据库表以匹配写的名字。我发现答案是后者。感谢您朝着正确的方向推动
      猜你喜欢
      • 2012-08-09
      • 1970-01-01
      • 2011-06-11
      • 1970-01-01
      • 2023-04-02
      • 2012-09-06
      • 1970-01-01
      • 2017-02-26
      • 1970-01-01
      相关资源
      最近更新 更多