我有一个动态增长的列表,当我添加任何项目到列表(在编辑文本和添加按钮的帮助下)。我有删除按钮,也它的工作完美,当我添加和删除。但在其他部分,我添加了编辑文本与文本监视器,当我搜索的东西,它排序列表和并行,如果我从列表中删除任何项目,它从列表中删除项目,但不刷新适配器,即使我调用notifyDataSetChanged()也。代码如下:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mArrayList = new ArrayList();
mEtSearch = (EditText) findViewById(R.id.et_search);
mEtText = (EditText) findViewById(R.id.et_item_to_add);
mBtnAdd = (Button) findViewById(R.id.btn_add);
mBtnDelete = (Button) findViewById(R.id.btn_delete);
mLvItems = (ListView) findViewById(R.id.lv_itmes);
mBtnAdd.setOnClickListener(this);
mBtnDelete.setOnClickListener(this);
mAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_multiple_choice, mArrayList);
mLvItems.setAdapter(mAdapter);
mEtSearch.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
String text = mEtSearch.getText().toString().toLowerCase(Locale.getDefault());
mAdapter.getFilter().filter(text);
}
});
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_add:
if (mEtText.getText().toString().isEmpty()) {
Toast.makeText(this, "add something to list ", Toast.LENGTH_LONG).show();
} else {
mArrayList.add(mEtText.getText().toString());
mAdapter.notifyDataSetChanged();
mEtText.setText("");
}
break;
case R.id.btn_delete:
SparseBooleanArray checkedItemPositions = mLvItems.getCheckedItemPositions();
int itemCount = mLvItems.getCount();
for (int i = itemCount - 1; i >= 0; i--) {
if (checkedItemPositions.get(i)) {
mArrayList.remove(i);//This also I tried
// mAdapter.remove(mArrayList.get(i));//This also I tried
}
}
checkedItemPositions.clear();
mAdapter.notifyDataSetChanged();
break;
}
}
尝试在当前列表中完成更改后重新加载列表