2012-12-22 35 views
0

我在我的自定義列表視圖中有一個問題,當我搜索列表視圖的內容時,似乎顯示錯誤的項目。 (查看下面的截圖你就會明白)Checkable顯示錯誤的項目選擇(使用simpleCursor +自定義佈局)

enter image description hereenter image description hereenter image description here

  1. 搜索名稱爲AA和選擇兩行(AABBCCC和AABC)

  2. 我尋覓與鈉卻沒有選擇行(Jana和Nathan)。但它顯示爲已選中。

  3. 刪除所有的搜索項目,我刪除搜索後沒有選擇任何行。它應該顯示兩行(AABBCC和AABC),但它顯示了錯誤的行Jana。

我不知道如何重寫上述行爲。我必須根據_rowid而不是位置進行檢查。

哪個班級將跟蹤列表中選擇的項目?如何覆蓋上述行爲?

有兩列姓名和號碼。 我已經自定義了我的佈局(代碼如下),它實現了可檢查。

Search Name(EditText)  [Done Btn] 
--------------------------------------- 
Contact_name_1   
Phone_no 
--------------------------------------- 
Contact_name_2   
Phone_no2 
--------------------------------------- 
Contact_name_3   
Phone_no3 
--------------------------------------- 

自定義佈局,可檢查的項目..

public class CustomCheckableLists extends RelativeLayout implements Checkable { 

private Checkable mCheckable; 

public CustomCheckableLists(Context context) { 
    super(context); 
} 

public CustomCheckableLists(Context context, AttributeSet attrs) 
{ 
    super(context,attrs); 
} 

public boolean isChecked() { 
    return mCheckable == null ? false : mCheckable.isChecked(); 
    //return false; 
} 

public void setChecked(boolean checked) { 
    if(mCheckable != null) 
     mCheckable.setChecked(checked); 


} 

public void toggle() { 
    if(mCheckable != null) 
      mCheckable.toggle(); 
} 

@Override 
protected void onFinishInflate() { 
    super.onFinishInflate(); 

    //Find items id.. 

    Log.w("onFinishInflate","onFinishInflate"); 

    // Find Checkable child 
    int childCount = getChildCount(); 
    for (int i = 0; i < childCount; ++i) { 
     View v = getChildAt(i); 
     if (v instanceof Checkable) { 
      mCheckable = (Checkable) v; 
      break; 
     } 
    } 
} 

自定義XML佈局

<CustomCheckableLists 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="?android:attr/listPreferredItemHeight" 
    android:gravity="center_vertical" 
     android:orientation="horizontal" 
    > 

    <TextView 
    android:id="@+id/id" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="name" 
    android:visibility="invisible" 
    /> 

    <TextView 
    android:id="@+id/text1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="nuoo" 
    /> 

    <CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/phonenu" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:checkMark="?android:attr/textCheckMark" 
    android:gravity="center_vertical" 
    android:paddingLeft="3dp" 
    android:paddingRight="6dp" 
    android:paddingTop="25dip" 
    /> 

</CustomCheckableLists> 

聯繫活動課

String[] columns = new String[] { 
      ContactsContract.Contacts._ID, 
      ContactsContract.Contacts.DISPLAY_NAME, 
       ContactsContract.CommonDataKinds.Phone.NUMBER 
    }; 

    int[] to = new int[] { 
      R.id.id, 
      R.id.text1, 
      R.id.phonenu 
    }; 

    dataAdapter = new SimpleCursorAdapter(this, R.layout.custom_checkable_item, cursor, columns, to); 
    listView.setAdapter(dataAdapter); 

任何幫助表示讚賞。提前致謝。

編輯

每當任何名稱,查詢已經形成用戶搜索返回的搜索結果中。 代碼如下

searchNameOrNumber = (EditText) findViewById(R.id.searchText); 
    searchNameOrNumber.addTextChangedListener(new TextWatcher() 
    { 
     public void onTextChanged(CharSequence searchterm, int start, int before, int count) { 
      Log.w("Text Searched.. ", " " + searchterm); 
      dataAdapter.getFilter().filter(searchterm.toString()); 
      //dataAdapter.notifyDataSetChanged(); 
     } 

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

    dataAdapter.setFilterQueryProvider(new FilterQueryProvider() { 
     public Cursor runQuery(CharSequence constraint) { 
      Log.w("Searched Query..,", constraint.toString()); 
      ); 


      return getSearchedContacts(constraint.toString()); 
     } 
    }); 


public Cursor getSearchedContacts(String inputText) 
{ 
    Log.w("inputText",inputText); 
     Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI; 
     String[] projection = new String[] { 
       ContactsContract.Contacts._ID, 
       ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, 
        ContactsContract.CommonDataKinds.Phone.NUMBER 
     }; 
     String selection = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " like '%" + inputText + "%'"; 

     Cursor people = getContentResolver().query(uri, projection, selection, null, null); 

     int indexName = people.getColumnIndex(StructuredName.DISPLAY_NAME); 
     int indexNumber = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER); 
     int idIdx = people.getColumnIndexOrThrow(PhoneLookup._ID); 

     if(!people.moveToFirst()) 
     { 
      Log.w("No Cursor.., ","No cursor.., Cursor is empty.."); 
     } 

     do { 
      String id = people.getString(idIdx); 
      String name = people.getString(indexName); 
      String number = people.getString(indexNumber); 
      // Do work... 
     } while (people.moveToNext()); 

     return people; 


} 

回答

1

您需要創建一個自定義的適配器和跟蹤檢查的項目有。您看到此行爲的原因是因爲SimpleCursorAdapter正在回收視圖,並且不知道如何處理已檢查的狀態,只設置文本標籤,而保留之前的已檢查狀態。

另一種方式來看到這種行爲是具有比項目一屏多,檢查一些,然後滾動,你也會發現,隨機項目將選中/取消爲您滾動。

您可以查看如何在this answer得到這個工作的完整示例。

如果您使用此功能進行搜索,那麼每次過濾時,您都必須清除勾選的職位列表。

+0

@dmom謝謝,讓我試試。 – swastican