如何在单击活动类中的按钮时过滤适配器上微调器上的列表
Posted
技术标签:
【中文标题】如何在单击活动类中的按钮时过滤适配器上微调器上的列表【英文标题】:How to filter a list on the spinner on the adapter on the click of a button in the activity class 【发布时间】:2019-04-28 19:18:36 【问题描述】:我正在尝试过滤微调器上的列表。微调器位于适配器上。我的活动类上有一个按钮,单击该按钮时,无论用户选择了微调器上的什么项目,都应显示相应的列表。 我试图实现 Filterable 接口,但没有任何成功。 任何人都可以帮助我了解代码应该如何。我是 android 新手,因此苦苦挣扎。
public class Department_Adapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements Filterable
public static Context context;
private ArrayList<Department_model> arraylist;
private List<Department_model> models_list;
private ArrayAdapter<String> spinnerArrayAdapter;
private AddressFilter addressFilter;
public Department_Adapter(Context context, List<Department_model> department_modelList)
this.models_list = department_modelList;
this.arraylist = new ArrayList<Department_model>();
this.arraylist.addAll(department_modelList);
this.context = context;
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i)
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.department_childview, viewGroup, false);
ViewHolder vh = new ViewHolder(v);
return vh;
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i)
final ViewHolder myViewHolder = (ViewHolder) viewHolder;
final Department_model model = models_list.get(i);
myViewHolder.text.setText("Department");
String items[] = new String[models_list.size()];
Set<String> Dep_list = new HashSet<String>();
for (int j = 0; j < items.length; j++)
String ID[]= new String[models_list.size()];
ID[j] = models_list.get(j).getDepartment_ID();
items[j] = models_list.get(j).getDepartment_Name();
String[] unique = new HashSet<String>(Arrays.asList(items)).toArray(new String[0]);
spinnerArrayAdapter = new ArrayAdapter<String>(context, R.layout.department_list, unique);
spinnerArrayAdapter.setDropDownViewResource(R.layout.category_list);
myViewHolder.spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
String dep_name = parent.getItemAtPosition(position).toString();
@Override
public void onNothingSelected(AdapterView<?> parent)
);
myViewHolder.spinner.setAdapter(spinnerArrayAdapter);
@Override
public int getItemCount()
return models_list.size();
@Override
public Filter getFilter()
if (addressFilter == null)
addressFilter = new AddressFilter();
return addressFilter;
public class AddressFilter extends Filter
@Override
protected FilterResults performFiltering(CharSequence constraint)
String Dep_Id= String.valueOf(Long.parseLong(constraint.toString()));;
FilterResults results = new FilterResults();
ArrayList<Department_model> filtered_list = new ArrayList<Department_model>();
for(int i = 0; i<models_list.size(); i++ )
if(models_list.get(i).getDepartment_ID()== Dep_Id)
Department_model obj = filtered_list.get(i);
filtered_list.add(obj);
results.count = filtered_list.size();
results.values = filtered_list;
return results;
@Override
protected void publishResults(CharSequence constraint, FilterResults results)
models_list = (ArrayList<Department_model>)results.values;
notifyDataSetChanged();
public class ViewHolder extends RecyclerView.ViewHolder
TextView text, spinner_text;
Spinner spinner;
public ViewHolder(@NonNull View itemView)
super(itemView);
text = (TextView) itemView.findViewById(R.id.text);
spinner = (Spinner) itemView.findViewById(R.id.spinner);
spinner_text = (TextView) itemView.findViewById(R.id.spinner_text);
【问题讨论】:
试过我下面的解决方案了吗? 【参考方案1】:请尝试使用以下代码:看到您过滤的是相同列表而不是主列表。
@Override
protected FilterResults performFiltering(CharSequence constraint)
String Dep_Id= String.valueOf(Long.parseLong(constraint.toString()));;
FilterResults results = new FilterResults();
ArrayList<Department_model> filtered_list = new ArrayList<Department_model>();
for(int i = 0; i< arraylist.size(); i++ )
if(arraylist.get(i).getDepartment_ID()== Dep_Id)
Department_model obj = arraylist.get(i);
filtered_list.add(obj);
results.count = filtered_list.size();
results.values = filtered_list;
return results;
【讨论】:
感谢您的回答。我看到了我做错了什么。现在你能告诉我如何将这个过滤列表传递到我的活动屏幕上的按钮上吗? 您可以在 Adapter 类中创建一个接口并在您的 Activity 类中实现它,并将过滤后的列表从适配器传递给 Activity 。 @akansharattan 如果对您有帮助,请接受并支持我的回答。以上是关于如何在单击活动类中的按钮时过滤适配器上微调器上的列表的主要内容,如果未能解决你的问题,请参考以下文章
在真正的 Apple Watch 和无限微调器上运行应用程序
单击第二个活动上的按钮在视图上执行“单击”或“滚动到”时出错