I am trying to filter a list on the spinner. The spinner is situated on the adapter. I have a button on my activity class, and on the click of that button, whatever item on the spinner the user has selected, the corresponding list should be displayed. I have tried to implement the Filterable interface but without any success. Can anyone please help me as to how the code should be. I am new to Android and hence struggling.
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);
}
}
}
Please try with below code : See you have Filter same list rather main list.