首页 > 解决方案 > filter() 方法不适用于我的列表视图

问题描述

每当我从 EditText 中搜索名称时,它都会正确过滤掉名称,但无论如何它总是显示第一个列表项。例如,如果 John 是第一个列表项,并且我搜索 Mary(列表视图中确实存在),它仍会显示 John,但其余项目将消失。所以看起来过滤正在工作,但列表项没有相应地更新。

这是我的适配器:

public class ContactAdapter extends ArrayAdapter<Contact> {

    private ArrayList<Contact> items; 
    private Context adapterContext; 

    public ContactAdapter(Context context, ArrayList<Contact> items) {
        super(context, R.layout.list_item, items);
        adapterContext = context;
        this.items = items;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;

        try{
            Contact contact = items.get(position);

            if(v == null) { //if there isn't an existing view to be reused, the LayoutInflater service is called to instantiate the list_item layout previously used
                LayoutInflater vi = (LayoutInflater) adapterContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.list_item, null);
            }

            TextView contactName = (TextView) v.findViewById(R.id.textContactName);
            TextView contactNumber = (TextView) v.findViewById(R.id.textPhoneNumber);
            TextView contactCell = (TextView) v.findViewById(R.id.textCellNumber);
            TextView contactStreetAddress = (TextView) v.findViewById(R.id.textStreetAddress);
            TextView contactCityStateZip = (TextView) v.findViewById(R.id.textCityStateZip);
            Button b = (Button) v.findViewById(R.id.buttonDeleteContact);
            ImageView starBFF = (ImageView) v.findViewById(R.id.imageBFFStar);
            ImageView starBBFF = (ImageView) v.findViewById(R.id.imageBBFFStar);
            contactName.setText(contact.getContactName());
            contactNumber.setText("Home: " + contact.getPhoneNumber());
            contactCell.setText("Cell: " + contact.getCellNumber());
            contactStreetAddress.setText(contact.getStreetAddress());
            contactCityStateZip.setText(contact.getCity() + ", " + contact.getState() + " " + contact.getZipCode());
            b.setVisibility(View.INVISIBLE);
'''

The next code is the filtering
'''
private void initSearch(){
    //SEARCHBAR
    EditText filter = findViewById(R.id.search_bar);
    filter.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) {
            (ContactListActivity.this).adapter.getFilter().filter(charSequence);
        }

        @Override
        public void afterTextChanged(Editable editable) {

        }
    });
}
'''

标签: androidandroid-layout

解决方案


这是我的代码,你可以试试

public class brandAdapter extends RecyclerView.Adapter<brandAdapter.MyViewHolder> implements Filterable {

    List<BrandModel> brandModelList;
    Context context;
    chooseBrand choose;
    private java.util.List<BrandModel> brand_filter;

    public void setChooseBrand(brandAdapter.chooseBrand chooseBrandr) {
        this.choose = chooseBrandr;
    }

    public brandAdapter(List<BrandModel> brandModels , brandAdapter.chooseBrand chooseBrandr, Context context) {
        brandModelList = brandModels;
        this.context = context;
        this.choose = chooseBrandr;
        brand_filter = brandModelList;

    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.brand_item, parent, false);

        return new brandAdapter.MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, final int position) {
        final BrandModel brandModel = brand_filter.get(position);
        holder.brandName.setText(brandModel.getBrandName());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                choose.getBrand(String.valueOf(brandModel.getId()), brandModel.getBrandName());

            }
        });
    }

    @Override
    public int getItemCount() {
        return brand_filter.size();
    }

    @Override
    public Filter getFilter() {
        return new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence charSequence) {
                String charString = charSequence.toString();
                if (charString.isEmpty()) {
                    brand_filter = brandModelList;
                } else {
                    ArrayList<BrandModel> filtered = new ArrayList<>();
                    for (BrandModel row : brandModelList) {


                        if (row.getBrandName().toLowerCase().contains(charString.toLowerCase())) {
                            filtered.add(row);

                        }
                    }

                    brand_filter = filtered;
                }

                FilterResults filterResults = new FilterResults();
                filterResults.values = brand_filter;
                return filterResults;
            }

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


    public class MyViewHolder extends RecyclerView.ViewHolder {

        private TextView brandName;

        public MyViewHolder(View itemView) {
            super(itemView);
            brandName = (TextView) itemView.findViewById(R.id.brand_name);

        }
    }

    public interface chooseBrand {
        void getBrand(String id, String name);
    }

}

我希望它会和你一起工作


推荐阅读