2013-03-05 29 views
0

我試圖在android中選擇聯繫人API。聯繫人包括Android中的搜索選項

  • 當我使用以下方法(CONTENT_URI)時,它的工作正常。
  • 但我無法獲得任何搜索選項的接觸中API.Kindly任何一個可以共享的信息,你know.thanks ..

代碼在這裏

public void onActivityResult(int reqCode, int resultCode, Intent data) { 
super.onActivityResult(reqCode, resultCode, data); 

switch (reqCode) { 
case (PICK_CONTACT) : 
    if (resultCode == Activity.RESULT_OK) { 

    Uri contactData = data.getData(); 
    Cursor c = managedQuery(contactData, null, null, null, null); 
    if (c.moveToFirst()) { 


     String id =c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts._ID)); 

     String hasPhone =c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)); 

      if (hasPhone.equalsIgnoreCase("1")) { 
      Cursor phones = getContentResolver().query( 
         ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null, 
         ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ id, 
         null, null); 
      phones.moveToFirst(); 
      cNumber = phones.getString(phones.getColumnIndex("data1")); 

      System.out.println("number is:"+cNumber); 
      } 
     /*name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 

     Textcontact.setText(name);*/ 

     edt.setText(cNumber); 

    } 
    } 
    break; 
} 
} 

注: 我需要一個功能,就像android聯繫人搜索

+1

你不需要用'1'只是'equals'會做同樣的比較時使用'equalsIgnoreCase'。 – 2013-03-05 07:39:19

回答

2

你將不得不create a search interface

首先,您需要創建一個既具有EditText又具有ListView的XML佈局。

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

<!-- Pretty hint text, and maxLines --> 
<EditText android:id="@+building_list/search_box" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:hint="type to filter" 
    android:inputType="text" 
    android:maxLines="1"/> 

<!-- Set height to 0, and let the weight param expand it --> 
<!-- Note the use of the default ID! This lets us use a 
    ListActivity still! --> 
<ListView android:id="@android:id/list" 
    android:layout_width="fill_parent" 
    android:layout_height="0dip" 
    android:layout_weight="1" 
    /> 

</LinearLayout> 

這將使所有事情都正確地展開,在ListView上方有一個漂亮的EditText。接下來,像平常一樣創建一個ListActivity,但在onCreate()方法中添加一個setContentView()調用,以便使用我們最近聲明的佈局。請記住,我們特意使用android:id =「@ android:id/list」來標識ListView。這允許ListActivity知道我們想要在我們聲明的佈局中使用哪個ListView。

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.filterable_listview); 

    setListAdapter(new ArrayAdapter<String>(this, 
        android.R.layout.simple_list_item_1, 
        getStringArrayList()); 
} 

運行的應用程序現在應該顯示您以前的ListView,上面一個漂亮盒子。爲了讓這個盒子做一些事情,我們需要從它輸入,並使輸入過濾列表。雖然很多人都試圖手動完成此操作,但大多數ListView Adapter類都帶有一個Filter對象,可用於自動執行過濾。我們只需要將輸入從EditText輸入到過濾器。結果很簡單。要運行一個快速測試,這一行添加到您的onCreate()調用

adapter.getFilter().filter(s); 

請注意,您需要將您的ListAdapter保存到一個變量,使這項工作 - 我已經從早期到一個變量,名爲救了我的ArrayAdapter '適配器'。

下一步是從EditText獲取輸入。這實際上需要一些想法。你可以添加一個OnKeyListener()到你的EditText。但是,這個聽衆只會收到一些關鍵事件。例如,如果用戶輸入'wyw',則預測文本可能會推薦'眼睛'。在用戶選擇'wyw'或'eye'之前,你的OnKeyListener將不會收到關鍵事件。有些人可能更喜歡這種解決方案,但我發現它令人沮喪。我想要每一個關鍵事件,所以我選擇了過濾或不過濾。解決方案是一個TextWatcher。只需創建TextWatcher並將其添加到EditText,並在每次文本更改時向ListAdapter Filter傳遞過濾器請求。請記住刪除OnDestroy()中的TextWatcher!下面是最終的解決方案:

private EditText filterText = null; 
ArrayAdapter<String> adapter = null; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.filterable_listview); 

    filterText = (EditText) findViewById(R.id.search_box); 
    filterText.addTextChangedListener(filterTextWatcher); 

    setListAdapter(new ArrayAdapter<String>(this, 
       android.R.layout.simple_list_item_1, 
       getStringArrayList()); 
} 

private TextWatcher filterTextWatcher = new TextWatcher() { 

public void afterTextChanged(Editable s) { 
} 

public void beforeTextChanged(CharSequence s, int start, int count, 
     int after) { 
} 

public void onTextChanged(CharSequence s, int start, int before, 
     int count) { 
    adapter.getFilter().filter(s); 
} 

}; 

@Override 
protected void onDestroy() { 
    super.onDestroy(); 
    filterText.removeTextChangedListener(filterTextWatcher); 
}