添加项目会中断 getFilter() 方法
Adding an item breaks getFilter() method
问题:添加列表项会阻止筛选方法工作。否则过滤器工作正常。
预期:通过添加项目和筛选方法正确更新了项目和数据库。
已测试:
- 过滤当前列表有效并应用布局。
- 过滤列表上的过滤器起作用并应用布局。
- 没有约束的过滤器加载完整列表并应用布局。
- 在过滤器起作用之前添加项目并应用布局。
- 在过滤后添加项目并应用布局。
- 添加项目后过滤无法过滤结果并且不会对布局应用任何更改。未提供 运行 时间错误。
可能的解决方案:我以为我缺少对项目列表的分配以获取列表的更新版本。检查后,似乎添加项目和过滤方法都在获取更新的列表。我开始认为我不明白过滤器方法是如何工作的,而且我缺少过滤器需要刷新的方法或行。非常感谢有关如何找到我丢失的东西的建议。
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;
}
}
}
问题在于发布结果未引用项目列表并将结果正确投射为集合。
解决方案:
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
items.clear();
items.addAll((Collection<? extends ProjectItem>) results.values);
notifyDataSetChanged();
}
问题:添加列表项会阻止筛选方法工作。否则过滤器工作正常。
预期:通过添加项目和筛选方法正确更新了项目和数据库。
已测试:
- 过滤当前列表有效并应用布局。
- 过滤列表上的过滤器起作用并应用布局。
- 没有约束的过滤器加载完整列表并应用布局。
- 在过滤器起作用之前添加项目并应用布局。
- 在过滤后添加项目并应用布局。
- 添加项目后过滤无法过滤结果并且不会对布局应用任何更改。未提供 运行 时间错误。
可能的解决方案:我以为我缺少对项目列表的分配以获取列表的更新版本。检查后,似乎添加项目和过滤方法都在获取更新的列表。我开始认为我不明白过滤器方法是如何工作的,而且我缺少过滤器需要刷新的方法或行。非常感谢有关如何找到我丢失的东西的建议。
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;
}
}
}
问题在于发布结果未引用项目列表并将结果正确投射为集合。
解决方案:
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
items.clear();
items.addAll((Collection<? extends ProjectItem>) results.values);
notifyDataSetChanged();
}