我有一个编辑文本
<EditText
android:id="@+id/selectvalue"
style="@style/searchList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:focusable="true"
android:hint="@string/search_order_hint"
android:imeOptions="actionSearch"
android:padding="10dp"
android:singleLine="true"
android:textColorHint="@color/common_gray"
android:textCursorDrawable="@null"
android:visibility="invisible" />
我已将 TextWatcher 附加到此。
selectedValue.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
showKeyBoard(mContext, selectedValue);
return false;
}
});
selectedValue.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
@Override
public void afterTextChanged(Editable s) {
if (!selectedAssetType.equals(OrderBookTypes.MUTUAL_FUND_TYPE)) {
loadSearchFilterData();
}
}
});
public void showKeyBoard(Context context, EditText edit) {
InputMethodManager m = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
if (m != null) {
edit.requestFocus();
m.toggleSoftInput(0, InputMethodManager.SHOW_IMPLICIT);
}
}
private void loadSearchFilterData() {
if (!selectedValue.getText().toString().isEmpty()) {
String s = selectedValue.getText().toString();
List<BoOrderBookDao> filterList = new ArrayList<>();
for (int i = 0; i < sortedFilteredList.size(); i++) {
if (sortedFilteredList.get(i).getDpVal().toLowerCase().contains(s.toLowerCase())) {
filterList.add(sortedFilteredList.get(i));
}
}
orderBookModel.clear();
orderBookModel.addAll(filterList);
} else {
orderBookModel.clear();
orderBookModel.addAll(sortedFilteredList);
}
if (orderBookModel.size() == 0) {
setErrorMsg(mContext.getResources().getString(R.string.no_data_available_txt));
} else {
itemListLayout.setVisibility(View.VISIBLE);
errorScrollview.setVisibility(View.GONE);
}
if (orderAdapter != null) {
orderAdapter.requestType = requestType;
orderAdapter.notifyDataSetChanged();
}
}
但我无法在那里打字。我打字时不显示字符。如果我删除
orderBookModel.addAll(filterList);
行,那么我可以输入,但删除该行会破坏我的功能。谁能解决这个问题?
添加 orderAdapter.notifyDataSetChanged();在 orderBookModel 中添加数据后。
更新代码替换此代码:
private void loadSearchFilterData() {
if (!selectedValue.getText().toString().isEmpty()) {
String s = selectedValue.getText().toString();
List<BoOrderBookDao> filterList = new ArrayList<>();
for (int i = 0; i < sortedFilteredList.size(); i++) {
if (sortedFilteredList.get(i).getDpVal().toLowerCase().contains(s.toLowerCase())) {
filterList.add(sortedFilteredList.get(i));
}
}
orderBookModel.clear();
orderBookModel.addAll(filterList);
orderAdapter.notifyDataSetChanged(); // Add this
} else {
orderBookModel.clear();
orderBookModel.addAll(sortedFilteredList);
orderAdapter.notifyDataSetChanged(); // Add this
}
if (orderBookModel.size() == 0) {
setErrorMsg(mContext.getResources().getString(R.string.no_data_available_txt));
} else {
itemListLayout.setVisibility(View.VISIBLE);
errorScrollview.setVisibility(View.GONE);
}
if (orderAdapter != null) {
orderAdapter.requestType = requestType;
orderAdapter.notifyDataSetChanged();
}
}