添加项会中断getfilter()方法

11dmarpk  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(290)

问题:添加列表项会阻止筛选器方法工作。否则过滤器工作正常。
期望:通过additem和filter方法正确更新项和数据库。
测试:
对当前列表进行筛选并应用布局。
过滤列表上的过滤器工作并应用布局。
无约束过滤器加载完整列表并应用布局。
在过滤器工作并应用布局之前添加项。
在过滤器工作并应用布局之后添加项。
添加项后筛选无法筛选结果,并且不会对布局应用任何更改。未提供运行时错误。
可能的解决方案:我以为我错过了一个项目列表的分配,以获取列表的更新版本。检查之后,additem和filter方法似乎都在获取更新的列表。我开始觉得我不明白filter方法是如何工作的,我缺少了一个filter需要刷新的方法或行。如果你能给我一些建议,我会很感激的。

private void addProjectItem() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        View view = getLayoutInflater().inflate(R.layout.project_add_layout, null);

        final EditText title = view.findViewById(R.id.addProjectTitle);
        final EditText description = view.findViewById(R.id.addProjectDescription);

        builder.setNegativeButton(
                android.R.string.cancel,
                (dialog, which) -> dialog.cancel()
        );

        builder.setPositiveButton(
                android.R.string.ok,
                (dialog, which) -> {
                    if (!title.getText().toString().isEmpty() && !description.getText().toString().isEmpty()) {
                        ProjectItem item = new ProjectItem(
                                title.getText().toString(),
                                description.getText().toString(),
                                appDataManager.getUid(),
                                false
                        );

                        projectListManager.addItem(item);
                        adapter.updateItems(projectListManager.getList());
                    } else {
                        Toast.makeText(SecondActivity.this, projectAddError, Toast.LENGTH_SHORT).show();
                    }
                }
        );

        builder.setView(view);
        builder.show();
    }

    private class ProjectItemAdapter extends ArrayAdapter<ProjectItem> implements Filterable {
        private Context context;
        private List<ProjectItem> items;

        private ImageButton projectCompleteButton;
        private ImageButton projectDescriptionButton;
        private TextView itemTitle;
        private ImageButton projectJoinButton;

        private ProjectItemAdapter( Context context, List<ProjectItem> items) {
            super(context, -1, items);

            this.context = context;
            this.items = items;
        }

        @NonNull
        @Override
        public Filter getFilter() {
            return projectFilter;
        }

        private final Filter projectFilter = new Filter() {

            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                FilterResults results = new FilterResults();

                List<ProjectItem> found = new ArrayList<>();

                if(constraint == null || constraint.length() == 0) {
                    found.addAll(projectListManager.getList());
                } else {
                    String filterPattern = constraint.toString().toLowerCase().trim();

                    for(ProjectItem item : items){
                        if(item.getTitle().toLowerCase().contains(filterPattern)) {
                            found.add(item);
                        }
                    }
                }

                results.values = found;
                results.count = found.size();
                return results;
            }

            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {
                clear();
                addAll((List)results.values);
                notifyDataSetChanged();
            }
        };

        public void updateItems(List<ProjectItem> items) {
            this.items = items;
            notifyDataSetChanged();
        }

        @Override
        public int getCount() {
            return items.size();
        }

        @NonNull
        @Override
        public View getView(int position, View convertView, @NonNull ViewGroup parent) {
            // Removed as there is nothing that manipulates the list item.
            return convertView;
        }
    }
}
wqsoz72f

wqsoz72f1#

问题在于发布结果没有引用项目列表,并且将结果正确地强制转换为集合。
解决方案:

@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
    items.clear();

    items.addAll((Collection<? extends ProjectItem>) results.values);

    notifyDataSetChanged();
}

相关问题