联系选择器

时间:2011-11-29 04:03:36

标签: android autocompletetextview contactscontract

我一直在为Comp Eng Mobile App类介绍这个单一功能,大约一个星期,开始理解游标,内容解析器和适配器。但代码识别我从不使用游标,但我不知道如何将游标合并到这个程序中。

public class HelloAutoCompleteActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ContentResolver content = getContentResolver();
        Cursor cursor = content.query(Contacts.CONTENT_URI, PEOPLE_PROJECTION, null, null, ContactsContract.Contacts.DISPLAY_NAME + " ASC");
        AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete_country);
//      ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item, COUNTRIES);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item, PEOPLE_PROJECTION);
//      textView.ContactListAdapter adapter = new textView.ContactListAdapter(this, cursor);
        textView.setAdapter(adapter);
    }
    private static final String[] PEOPLE_PROJECTION = new String[] {
        ContactsContract.Contacts._ID,
        ContactsContract.CommonDataKinds.Phone.TYPE,
        ContactsContract.CommonDataKinds.Phone.NUMBER,
        ContactsContract.CommonDataKinds.Phone.LABEL,
        ContactsContract.Contacts.DISPLAY_NAME
    };
    String[] projection = new String[] {
            ContactsContract.Contacts._ID,
            ContactsContract.Contacts._COUNT,
            ContactsContract.Contacts.DISPLAY_NAME,
            ContactsContract.CommonDataKinds.Phone.NUMBER
         };

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

    ////Make the query. 
    Cursor cursor;
}

1 个答案:

答案 0 :(得分:0)

正如您所提到的,您开始了解Cursor,因此您知道游标的基本功能,它用于保存Query在数据库上返回的数据。因为在上面的代码中,您正在查询设备的Contact数据库。并且代码返回游标中的数据,因为您没有进一步使用它。您可以使用光标并在下面执行

  1. 获取联系人姓名并在屏幕上显示
  2. 取联系人姓名和号码并显示。
  3. 下面是示例程序的链接,导入它你会得到更好的想法。

    http://l8rs.blogspot.com/2009/03/android-sample-list-contact-book-and.html