Fundapter列表视图过滤错误

时间:2017-07-03 04:20:27

标签: android

我正在尝试在列表视图中执行搜索功能,而我正在使用Fundapter而不是正常Adapter。但是当我尝试在编辑文本中输入内容时,我得到:

  

尝试在空对象引用上调用虚方法'void android.widget.Filter.filter(java.lang.CharSequence)'

这是我的代码:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_view, container, false);

    final ArrayList<Participants> participants = new ArrayList<>();

    //then i hardcoded the participants data...

    //participants.add(p1);

    BindDictionary<Participants> dictionary = new BindDictionary<>();
    dictionary.addStringField(R.id.tvName, new StringExtractor<Participants>() {
        @Override
        public String getStringValue(Participants participants, int position) {
            return participants.getName();
        }
    });

    dictionary.addStringField(R.id.tvCompany, new StringExtractor<Participants>() {
        @Override
        public String getStringValue(Participants participants, int position) {
            return participants.getCompanyName();
        }
    });

    dictionary.addStringField(R.id.tvNRIC, new StringExtractor<Participants>() {
        @Override
        public String getStringValue(Participants participants, int position) {
            return participants.getNRIC();
        }
    });


    final FunDapter adapter = new FunDapter(ViewFragment.this.getActivity(), participants, R.layout.participants_layout, dictionary);

    ListView lvParticipants = (ListView) view.findViewById(R.id.lvParticipants);
    lvParticipants.setAdapter(adapter);
    etSearch = (EditText) view.findViewById(R.id.etSearch);

    etSearch.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            adapter.getFilter().filter(s);
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });

    lvParticipants.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Participants selectedParticipants = participants.get(position);
            Toast.makeText(ViewFragment.this.getActivity(), selectedParticipants.getName(), Toast.LENGTH_SHORT).show();
        }
    });

    return view;

}

1 个答案:

答案 0 :(得分:1)

不应该是onTextChnaged(),你正在beforeTextChnaged()进行操作......看看它。