我的自定义列表视图中有一个问题,当我搜索列表视图的内容时,它似乎显示选择了错误的项目。(看看下面的截图你就明白了)
用名称 aa 搜索并选择了两行(AABBCCC 和 AABC)
我用 Na 搜索过,但没有选择行(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;
}