您必须addTextChangedListener
添加TextWatcher
.
Boolean[] isPresent;
ed = (EditText) findViewById(R.id.editText1);
ed.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
}
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
textLength = ed.getText().length();
list.clear();
isPresent = initIsPresentArray();
for (int i = 0; i < tempList.length; i++) {
for (int j = 0; j < (tempList[i].length() - (textLength - 1)); j++) {
if (textLength <= tempList[i].length()) {
if (isPresent[i] == false) {
if (ed.getText()
.toString()
.equalsIgnoreCase(
(String) tempList[i]
.subSequence(
j,
(j + textLength)))) {
list.add(tempCaseList.get(i));
isPresent[i] = true;
}
}
}
}
if (list.size() == 0) {
error.setText(getResources().getString(R.string.notFound));
} else {
error.setText("");
}
lv.setAdapter(new CustomAdapter(History.this, list));
}
}
});
和方法 initIsPresentArray()
public Boolean[] initIsPresentArray() {
Boolean[] isPresent = new Boolean[tempCaseList.size()];
for (int i = 0; i < isPresent.length; i++) {
isPresent[i] = false;
}
return isPresent;
}
如您所见,我不直接对ArrayList
包含信息进行操作,而是使用此方法将其复制ArrayList
到一个临时的ArrayList
public ArrayList<Case> createTempList(ArrayList<Case> listOfCases) {
ArrayList<Case> temporaryList = new ArrayList<Case>();
for (Case c : listOfCases) {
temporaryList.add(c);
}
return temporaryList;
}
如果有不清楚的地方,只需添加评论!