Android listview не может быть обновлен с помощью getFilter () в Android

#android #listview #custom-adapter

#Android #listview #пользовательский адаптер

Вопрос:

Я хочу создать счетчик с возможностью поиска и использую пользовательский listview, но когда я использую getFilter (), список не может быть обновлен с помощью отфильтрованного результата, хотя я отлаживаю результат, и он получает правильный размер данных

Это мой код :

Пользовательский адаптер :

 public class BranchesSpinnerAdapter extends ArrayAdapter<BranchesSpinnerModel> {

    List<BranchesSpinnerModel> branchesSpinnerLists = new ArrayList<>();
    private ArrayList<HashMap<String, String>> filteredData;
    Context context;
    public BranchesSpinnerAdapter(Context context, int viewResourceId) {
        super(context, viewResourceId);
        this.context = context;
    }

    public void setList(List<BranchesSpinnerModel> branchesSpinnerLists) {
        this.branchesSpinnerLists = branchesSpinnerLists;
        notifyDataSetChanged();
    }

    public int getCount()
    {
        return filteredData.size();
    }

    public long getItemId(int position)
    {
        return position;
    }

    public View getCustomView(int position, View convertView,
                              ViewGroup parent) {

        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        BranchesSpinnerItemBinding binding = DataBindingUtil.inflate(inflater, R.layout.branches_spinner_item, parent, false);

        BranchesSpinnerModel bs = branchesSpinnerLists.get(position);

        binding.txTitle.setText(bs.getTitle());
        return binding.getRoot();
    }



    // It gets a View that displays in the drop down popup the data at the specified position
    @Override
    public View getDropDownView(int position, View convertView,
                                ViewGroup parent) {
        return getCustomView(position, convertView, parent);
    }

    // It gets a View that displays the data at the specified position
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        return getCustomView(position, convertView, parent);
    }

    @Override
    public Filter getFilter()
    {
        return new Filter()
        {
            @Override
            protected FilterResults performFiltering(CharSequence charSequence)
            {
                FilterResults results = new FilterResults();

                //If there's nothing to filter on, return the original data for your list
                if(charSequence == null || charSequence.length() == 0)
                {
                    results.values = branchesSpinnerLists;
                    results.count = branchesSpinnerLists.size();
                }
                else
                {
                    ArrayList<String> filterResultsData = new ArrayList<>();

                    for(BranchesSpinnerModel data : branchesSpinnerLists)
                    {
                        //In this loop, you'll filter through originalData and compare each item to charSequence.
                        //If you find a match, add it to your new ArrayList
                        //I'm not sure how you're going to do comparison, so you'll need to fill out this conditional
                        if(data.getTitle().contains(charSequence))
                        {
                            Log.e("charfilter", "true");
                            filterResultsData.add(data.getTitle());
                            Log.e("filterResultsData", String.valueOf(filterResultsData.size()));
                        }
                    }

                    results.values = filteredData;
                    results.count = filterResultsData.size();
                }

                return results;
            }

            @Override
            protected void publishResults(CharSequence charSequence, FilterResults filterResults)
            {
                filteredData = (ArrayList<HashMap<String,String>>)filterResults.values;
                notifyDataSetChanged();
            }
        };
    }


}
  

Мой код во фрагменте :

 branchesSpinnerAdapter = new BranchesSpinnerAdapter(getContext(), 0);
        binding.branchesList.setAdapter(branchesSpinnerAdapter);


        findCarViewModel.branchesSpinnerMutableLiveData.observe(getViewLifecycleOwner(), new Observer<List<BranchesSpinnerModel>>() {
            @Override
            public void onChanged(List<BranchesSpinnerModel> branchesSpinnerModel) {
                branchesSpinnerAdapter.setList(branchesSpinnerModel);
                branchesSpinnerAdapter.addAll(branchesSpinnerModel);
            }
        });

        binding.receiptPlace.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog = new Dialog(getActivity());
                dialog.setContentView(R.layout.dialog_spinner_searchable);
                dialog.getWindow().setLayout(950, 1200);
                dialog.show();

                EditText edittext = dialog.findViewById(R.id.spinnerSearch);
                ListView lv = dialog.findViewById(R.id.spinnerListview);
                lv.setAdapter(branchesSpinnerAdapter);

                edittext.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                    }

                    @Override
                    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                        branchesSpinnerAdapter.getFilter().filter(charSequence);
                        branchesSpinnerAdapter.notifyDataSetChanged();
                    }

                    @Override
                    public void afterTextChanged(Editable editable) {

                    }
                });

                lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                        binding.receiptPlace.setText(branchesSpinnerAdapter.getItem(i).getTitle());
                        dialog.dismiss();
                    }
                });
            }
        });
  

Итак, как я могу исправить это, чтобы показывать отфильтрованные данные при вводе в поле поиска

Ответ №1:

Во фрагменте нет необходимости иметь branchesSpinnerAdapter.notifyDataSetChanged(); и пробовать этот код адаптера:

 public class BranchesSpinnerAdapter extends ArrayAdapter<BranchesSpinnerModel> {

List<BranchesSpinnerModel> branchesSpinnerLists = new ArrayList<>();
private List<BranchesSpinnerModel> filteredData = new ArrayList<>(); // Changed
Context context;
public BranchesSpinnerAdapter(Context context, int viewResourceId) {
    super(context, viewResourceId);
    this.context = context;
}

public void setList(List<BranchesSpinnerModel> branchesSpinnerLists) {
    this.branchesSpinnerLists = branchesSpinnerLists;
    filteredData = new ArrayList<>(branchesSpinnerLists);   // Added
    notifyDataSetChanged();
}

public int getCount()
{
    return filteredData.size();
}

public long getItemId(int position)
{
    return position;
}

public View getCustomView(int position, View convertView,
                          ViewGroup parent) {

    LayoutInflater inflater = LayoutInflater.from(parent.getContext());
    BranchesSpinnerItemBinding binding = DataBindingUtil.inflate(inflater, R.layout.branches_spinner_item, parent, false);

    // BranchesSpinnerModel bs = branchesSpinnerLists.get(position);
    BranchesSpinnerModel bs = filteredData.get(position);   // Changed

    binding.txTitle.setText(bs.getTitle());
    return binding.getRoot();
}

// It gets a View that displays in the drop down popup the data at the specified position
@Override
public View getDropDownView(int position, View convertView,
                            ViewGroup parent) {
    return getCustomView(position, convertView, parent);
}

// It gets a View that displays the data at the specified position
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    return getCustomView(position, convertView, parent);
}

@Override
public Filter getFilter()
{
    return new Filter()
    {
        @Override
        protected FilterResults performFiltering(CharSequence charSequence)
        {
            FilterResults results = new FilterResults();

            //If there's nothing to filter on, return the original data for your list
            if(charSequence == null || charSequence.length() == 0)
            {
                results.values = branchesSpinnerLists;
                results.count = branchesSpinnerLists.size();
            }
            else
            {
                ArrayList<BranchesSpinnerModel> filterResultsData = new ArrayList<>();

                for(BranchesSpinnerModel data : branchesSpinnerLists)
                {
                    //In this loop, you'll filter through originalData and compare each item to charSequence.
                    //If you find a match, add it to your new ArrayList
                    //I'm not sure how you're going to do comparison, so you'll need to fill out this conditional
                    if(data.getTitle().contains(charSequence))
                    {
                        Log.e("charfilter", "true");
                        filterResultsData.add(data);    // Changed
                        Log.e("filterResultsData", String.valueOf(filterResultsData.size()));
                    }
                }

                results.values = filteredData;
                results.count = filterResultsData.size();
            }

            return results;
        }

        @Override
        protected void publishResults(CharSequence charSequence, FilterResults filterResults)
        {
            filteredData.clear();   // Added
            filteredData.addAll((ArrayList<BranchesSpinnerModel>)filterResults.values); // Changed
            notifyDataSetChanged();
        }
    };
}

}