带有 SimpleCursorAdapter 的 AutoCompleteTextView 用于加载联系人

Posted

技术标签:

【中文标题】带有 SimpleCursorAdapter 的 AutoCompleteTextView 用于加载联系人【英文标题】:AutoCompleteTextView with SimpleCursorAdapter for loading contacts 【发布时间】:2014-03-13 08:25:55 【问题描述】:

您好,我正在开发一个 android SMS 应用程序,其中我使用 AutoCompleteTextView 来使用 SimpleCursorAdapter 搜索联系人,如下代码所示。

    mTxtPhoneNo = (AutoCompleteTextView) findViewById(R.id.editTextContact);
    mTxtPhoneNo.setOnClickListener(this);

    phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
              ContactsContract.CommonDataKinds.Phone.CONTACT_ID ,null, null);
    String[] columns = new String[] 
            
            ContactsContract.Contacts.DISPLAY_NAME,
            ContactsContract.CommonDataKinds.Phone.NUMBER,
            ContactsContract.CommonDataKinds.Phone.TYPE

            ;

    adapter  = new SimpleCursorAdapter(this, R.layout.custcontview, phones ,columns, new int[]  R.id.ccontName, R.id.ccontNo, R.id.ccontType );
    mTxtPhoneNo.setAdapter(adapter);

搜索字符串可以是号码或联系人姓名。如何使用 SimpleCursorAdapter 进行搜索。我对 CursorAdapter 很陌生,对此知之甚少。请提供任何相关示例代码或相关链接的帮助。

谢谢!

【问题讨论】:

这个链接可能对你有帮助..developer.android.com/reference/android/app/LoaderManager.html 【参考方案1】:
public class ContactActivity extends Activity 

    private ArrayList<Map<String, String>> mPeopleList;

    private SimpleAdapter mAdapter;
    private AutoCompleteTextView mTxtPhoneNo;

/** Called when the activity is first created. */

    @Override
    public void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mPeopleList = new ArrayList<Map<String, String>>();
        PopulatePeopleList();
        mTxtPhoneNo = (AutoCompleteTextView) findViewById(R.id.mmWhoNo);

        mAdapter = new SimpleAdapter(this, mPeopleList, R.layout.custcontview ,new String[]  "Name", "Phone" , "Type" , new int[]  R.id.ccontName, R.id.ccontNo, R.id.ccontType );

        mTxtPhoneNo.setAdapter(mAdapter);

        

    public void PopulatePeopleList()
    

    mPeopleList.clear();

    Cursor people = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);

    while (people.moveToNext())
    
    String contactName = people.getString(people.getColumnIndex(
    ContactsContract.Contacts.DISPLAY_NAME));

    String contactId = people.getString(people.getColumnIndex(
    ContactsContract.Contacts._ID));
    String hasPhone = people.getString(people.getColumnIndex(
    ContactsContract.Contacts.HAS_PHONE_NUMBER));

    if ((Integer.parseInt(hasPhone) > 0))
    

    // You know have the number so now query it like this
    Cursor phones = getContentResolver().query(
    ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
    null,
    ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,
    null, null);
    while (phones.moveToNext()) 

    //store numbers and display a dialog letting the user select which.
    String phoneNumber = phones.getString(
    phones.getColumnIndex(
    ContactsContract.CommonDataKinds.Phone.NUMBER));

    String numberType = phones.getString(phones.getColumnIndex(
    ContactsContract.CommonDataKinds.Phone.TYPE));

    Map<String, String> NamePhoneType = new HashMap<String, String>();

    NamePhoneType.put("Name", contactName);
    NamePhoneType.put("Phone", phoneNumber);

    if(numberType.equals("0"))
    NamePhoneType.put("Type", "Work");
    else
    if(numberType.equals("1"))
    NamePhoneType.put("Type", "Home");
    else if(numberType.equals("2"))
    NamePhoneType.put("Type",  "Mobile");
    else
    NamePhoneType.put("Type", "Other");

    //Then add this map to the list.
    mPeopleList.add(NamePhoneType);
    
    phones.close();
    
    
    people.close();

    startManagingCursor(people);
    
    

【讨论】:

我以前用过这个。这加载活动非常缓慢,几乎像 7 到 8 秒。如何使用光标适配器更快地加载它 androidexample.com/… 感谢您的链接。但甚至会使活动负载变慢。如何解决? 等一下,我会回复你的工作 @DigveshPatel,我遇到了同样的问题,即活动负载缓慢。你能帮忙吗??

以上是关于带有 SimpleCursorAdapter 的 AutoCompleteTextView 用于加载联系人的主要内容,如果未能解决你的问题,请参考以下文章

ListView 没有向我显示包含数据库名称的列表。我使用 SimpleCursorAdapter

SimpleCursorAdapter无法在MainActivity中使用

使用 EditText 过滤 SimpleCursorAdapter 支持的 ListView

SimpleCursorAdapter 在 bindView 上重复单击操作

使用 SimpleCursorAdapter 基于 sqlite 查询更新列表视图

更新 SimpleCursorAdapter,同时保持 ListView 中的滚动位置