我在我的自定義列表視圖中有一個問題,當我搜索列表視圖的內容時,似乎顯示錯誤的項目。 (查看下面的截圖你就會明白)Checkable顯示錯誤的項目選擇(使用simpleCursor +自定義佈局)
搜索名稱爲AA和選擇兩行(AABBCCC和AABC)
我尋覓與鈉卻沒有選擇行(Jana和Nathan)。但它顯示爲已選中。
刪除所有的搜索項目,我刪除搜索後沒有選擇任何行。它應該顯示兩行(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;
}
@dmom謝謝,讓我試試。 – swastican