首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在使用动态列表和文本观察器时,通知数据集更改不起作用

在使用动态列表和文本观察器时,通知数据集更改不起作用
EN

Stack Overflow用户
提问于 2016-11-01 12:07:36
回答 1查看 181关注 0票数 3

我有一个列表,它动态增长,当我添加任何项目到列表(在编辑文本和添加按钮的帮助下),.I有删除按钮,也可以完美地工作,当我添加和deleting.But在其他部分我添加了编辑文本与文本观察器,当我搜索它排序列表和并行,如果我从列表中删除任何项目,它从列表中删除项目,但不刷新适配器,即使我也调用了notifyDataSetChanged()。下面是代码。

代码语言:javascript
运行
复制
@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;

    }

}
EN

回答 1

Stack Overflow用户

发布于 2017-05-05 17:39:37

在当前列表中完成更改后,请尝试重新加载列表。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40354008

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档