ListView在edittext中过滤数据

时间:2017-03-23 04:25:16

标签: android listview

我创建了一个使用自定义适配器的listview,我想通过输入edittext来过滤listview。但是,我发现很难,因为我从Firebase获取了所有数据。我怎么能按名称过滤数据?我需要添加什么代码?

这是我的主要活动

private void readFirebaseData() {
    FirebaseDatabase.getInstance().getReference("account").addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                uid = (String) snapshot.child("uid").getValue();
                contactNo = (String) snapshot.child("contactNo").getValue();

                email = (String) snapshot.child("email").getValue();
                name = (String) snapshot.child("name").getValue();

                Shop shops = new Shop(uid, contactNo, email, name);
                shopListAdapter adapter = new shopListAdapter(getActivity(), shop);
                adapter.add(shops);


                listView.setAdapter(adapter);
                adapter.notifyDataSetChanged();
            }
        }



}
private void filter() {
    searcher.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            adapter.getFilter().filter(s);
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });
}

这是我的适配器类

public class shopListAdapter extends ArrayAdapter<Shop> {

private ArrayList<Shop> shops;
Filter filter;

public shopListAdapter(Context context, ArrayList<Shop> shops) {
    super(context, 0, shops);

}

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

    Shop shops = getItem(position);

    if (convertView == null) {
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.item, parent, false);
    }

    TextView name = (TextView) convertView.findViewById(R.id.txtName);
    TextView address = (TextView) convertView.findViewById(R.id.txtAdd);

    name.setText(shops.getShopName());
    address.setText(shops.getAddress());


    return convertView;
}

1 个答案:

答案 0 :(得分:0)

Plz尝试添加这个。运行代码是不正确的

public class MyLeadsAdapter extends ArrayAdapter<MyleadsListModel> {

private Vector<MyleadsListModel> originalList;
private Vector<MyleadsListModel> numberList;
private CountryFilter filter;
private Context context;
public MyLeadsAdapter(Context context, int textViewResourceId, Vector<MyleadsListModel> numberList) {
    super(context, textViewResourceId, numberList);
    this.context = context;
    this.numberList = new Vector<MyleadsListModel>();
    this.numberList.addAll(numberList);
    this.originalList = new Vector<MyleadsListModel>();
    this.originalList.addAll(numberList);
}

@Override
public Filter getFilter() {
    if (filter == null) {
        filter = new CountryFilter();
    }
    return filter;
}


@Override
public View getView(final int position, View convertView,
                    ViewGroup parent) {

    // TODO Auto-generated method stub
    final MyViewHolder mViewHolder;

    LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.row_my_leads,
                null);

        convertView.setTag(mViewHolder);

    } else {
        mViewHolder = (MyViewHolder) convertView.getTag();
    }



    return convertView;

}

class MyViewHolder {

}

private class CountryFilter extends Filter {

    @Override
    protected FilterResults performFiltering(CharSequence constraint) {

        constraint = constraint.toString().toLowerCase();
        FilterResults result = new FilterResults();
        if (constraint != null && constraint.toString().length() > 0) {
            Vector<MyleadsListModel> filteredItems = new Vector<MyleadsListModel>();

            for (int i = 0, l = originalList.size(); i < l; i++) {
                MyleadsListModel contactList = originalList.get(i);
                if (contactList.getName().toString().toLowerCase()
                        .contains(constraint))
                    filteredItems.add(contactList);
            }
            result.count = filteredItems.size();
            result.values = filteredItems;
        } else {
            synchronized (this) {
                result.values = originalList;
                result.count = originalList.size();
            }
        }
        return result;
    }

    @SuppressWarnings("unchecked")
    @Override
    protected void publishResults(CharSequence constraint,
                                  FilterResults results) {
        numberList = (Vector<MyleadsListModel>) results.values;
        notifyDataSetChanged();
        clear();
        for (int i = 0, l = numberList.size(); i < l; i++)
            add(numberList.get(i));
        notifyDataSetInvalidated();
    }
}

}

为Edittext添加此代码:

searcher.addTextChangedListener(new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        adapter.getFilter().filter(s);
    }

    @Override
    public void afterTextChanged(Editable s) {

    }
});