我有一个带有阿拉伯字符串的自定义列表视图。我想在该列表视图中搜索特定视图(行)。我在如下所示的主要活动中使用addTextChangedListener
editsearch = (EditText) findViewById(R.id.search);
editsearch.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
String text = editsearch.getText().toString().toLowerCase(Locale.ENGLISH.getDefault());
//String text = editsearch.getText().toString();
adapter.filter(text);
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1,int arg2, int arg3) {
// TODO Auto-generated method stub
}
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2,int arg3) {
// TODO Auto-generated method stub
}
});
在我的adpater中,我使用基本适配器
Context mContext;
LayoutInflater inflater;
private List<MyDataModelads2> modelList = null;
private ArrayList<MyDataModelads2> arraylist;
public myarraynew(Context context, List<MyDataModelads2> modelList) {
mContext = context;
this.modelList = modelList;
inflater = LayoutInflater.from(mContext);
this.arraylist = new ArrayList<MyDataModelads2>();
this.arraylist.addAll(modelList);
}
然后我在适配器中插入了以下过滤器方法
public void filter(String charText) {
charText = charText.toLowerCase(Locale.getDefault());
modelList.clear();
if (charText.length() == 0) {
modelList.addAll(arraylist);
} else {
for (MyDataModelads2 wp : arraylist) {
if (wp.getName().toLowerCase(Locale.getDefault()).contains(charText)) {
modelList.add(wp);
}
}
}
notifyDataSetChanged();
}
有两个问题: