Android listview 不能用 android 中的 getFilter() 更新

Posted

技术标签:

【中文标题】Android listview 不能用 android 中的 getFilter() 更新【英文标题】:Android listview can't be updated with getFilter() in android 【发布时间】:2021-01-22 13:26:47 【问题描述】:

我想制作可搜索的微调器,我使用自定义列表视图,但是当我使用 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();
        
    ;



【讨论】:

以上是关于Android listview 不能用 android 中的 getFilter() 更新的主要内容,如果未能解决你的问题,请参考以下文章

android listview不能刷新 adapter.notifyDataSetChanged()和setListAdapter(myAdapter)都不好用

如何设置 ListView 的最大高度?

如何调整 ListView 中的 ImageView?

如何使 ListView 定位在某个元素下?

Android 中实现模拟搜索的功能详解

ListView 不使用自定义视图更新大小