如何使用SearchView按字符而不是精确单词过滤ListView?

时间:2018-01-01 06:19:52

标签: android listview

我有一个SearchView(v7)和Listview。一旦用户在SearchView中输入任何内容,结果就会产生,我会在Listview中填充。所有工作但问题是我必须准确写出Listview中存在的那个词

现在,我希望当用户输入我们的ListView数据中匹配结果将显示的任何匹配字符时。

例如,当我输入字母I时,结果将会跟随。

1)mitesh

2)vidhi

3)ravi

4)Ravi P

5)Tushar prajapati

现在大写和小写字母无关紧要。我怎样才能做到这一点?

3 个答案:

答案 0 :(得分:0)

试试这段代码:

 TextWatcher edtSearchTextTextWatcher = new TextWatcher() {

        public void afterTextChanged(Editable s) {
        }

        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        public void onTextChanged(CharSequence s, int start, int before, int count) {
            String text = edtSearchText.getText().toString().trim();
            onSearchTextChanged(text);
        }
    };

    private void onSearchTextChanged(String text) {
        if (adapter!= null)
            adapter.filter(text);
    }


public void filter(final String text) {

        // Searching could be complex..so we will dispatch it to a different thread...
        new Thread(new Runnable() {
            @Override
            public void run() {

                // Clear the filter list
                filterList.clear();

                // If there is no search value, then add all original list items to filter list
                if (TextUtils.isEmpty(text)) {

                    filterList.addAll(inspectionsList);

                } else {
                    // Iterate in the original List and add it to filter list...
                    for (ClsInspection item : inspectionsList) {
                        if (item.getClientName().toLowerCase().contains(text.toLowerCase()) || item.getInspectorsName().toLowerCase().contains(text.toLowerCase())
                                || item.getAddesss().toLowerCase().contains(text.toLowerCase())) {
                            // Adding Matched items
                            filterList.add(item);
                        }
                    }
                }

                // Set on UI Thread
                (fragment.getActivity()).runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        // Notify the List that the DataSet has changed...
                        notifyDataSetChanged();
                    }
                });

            }
        }).start();

    }


private List<String> inspectionsList = new ArrayList<>();
private List<String> filterList = new ArrayList<>();

public adapter( List<String> inspectionsList) {
        this.inspectionsList = inspectionsList;
        this.filterList.addAll(inspectionsList);
    }

答案 1 :(得分:0)

首先你应该使用RecylerView而不是ListView。并在onQueryTextChange方法内使用此代码

            @Override
            public boolean onQueryTextChange(String newText) {

                newText = newText.toLowerCase() ;
                List<String> searchnames = new ArrayList<>() ;

                for(int i = 0 ; i < names.length ; i++){
                    String name = names[i].toLowerCase() ;
                    if(name.contains(newText)){
                        searchnames.add(name) ;
                    }
                }
                sampleRecylerAdapter.setFilter(searchnames);
                return true;
            }

这里,names是带有字符串集合的数组。和sampleRecylerAdapter.setFilter(searchnames);这里setFilter()是RecylerViewAdapter类的方法。

这里是RecylerviewAdapter的所有代码:

public class SampleRecylerAdapter extends RecyclerView.Adapter<SampleRecylerAdapter.SampleRecylerViewHolder> {

    private final LayoutInflater inflater;
    private Context context ;
    private  List<String> name = Collections.emptyList() ;


    public SampleRecylerAdapter(Context context, List<String> name){
        this.context = context ;
        this.name = name ;
        inflater = LayoutInflater.from(context) ;
    }

    @Override
    public SampleRecylerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = inflater.inflate(R.layout.item_names, parent, false) ;
        SampleRecylerViewHolder sampleRecylerViewHolder = new SampleRecylerViewHolder(view);
        return sampleRecylerViewHolder;
    }

    @Override
    public void onBindViewHolder(SampleRecylerViewHolder holder, int position) {
        holder.textView.setText(name.get(position));
    }

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

    public class SampleRecylerViewHolder extends RecyclerView.ViewHolder{

        TextView textView ;
        public SampleRecylerViewHolder(View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.mynames) ;
        }
    }

    public void setFilter(List<String> mynames){
        name = new ArrayList<>() ;
        name.addAll(mynames) ;
        notifyDataSetChanged();
    }
}

尝试以这种方式使用您的代码。我希望它可以帮助你解决问题。如果你有任何问题,请告诉我。

答案 2 :(得分:0)

适用于我在这种情况下使用过的任何文本侦听器

messageBoxView.alertdialog_edittext.addTextChangedListener(object : TextWatcher {
        override fun afterTextChanged(s: Editable?) {
            //adapter.getFilter().filter(s) ;

            val sr = s.toString().toLowerCase(Locale.ROOT)
            val newList: ArrayList<String> = ArrayList()
            //listItems is current list<String> inside adapter before searching
            for (list_item in listItems){
                val nocaps_item = list_item?.toLowerCase(Locale.ROOT)
                if(nocaps_item?.contains(sr)!!){
                    newList.add(list_item)
                }
            }
            val adapterNew: ArrayAdapter<String?> = ArrayAdapter(
                requireContext(),
                android.R.layout.simple_list_item_single_choice,
                newList as List<String?>
            )
            messageBoxView.alertdialog_Listview.adapter = adapterNew

        }

        override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
        }

        override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
        }
    })