如何在 android 中的简单列表视图上使用 startsWith 应用搜索过滤器

How to apply search filter using startsWith on a simple listview in android

我有一个 ListView 从数据库获取数据。我在上面应用了 搜索过滤器 ,但它没有按照我的意愿进行过滤。我想要的是过滤 第一个字母 而不是中间或包含。

代码如下:

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

    ListView dicList; 
    EditText editSearch;
    ArrayAdapter<String> adapterListOfWord;
    ArrayList<String> mSource = new ArrayList<String>();

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        editSearch = findViewById(R.id.edit_search);
        dicList = findViewById(R.id.dic_list);

        adapterListOfWord = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mSource);
        dicList.setAdapter(adapterListOfWord);

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

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int start, int before, int count) {
                // the problem is here, i want to search with the first letter not middle or contains.
                adapterListOfWord.getFilter().filter(charSequence);
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }
}

我不知道如何根据我的代码实现和应用 filterable 到我的项目,并将 contains() 更改为 startsWith() 以实现我的目标。
非常感谢您的帮助。
提前致谢。

您需要使用自定义适配器而不是默认适配器,并覆盖 getFilter() 以使用 String 的 startsWith() 方法过滤列表:

适配器:

public class StartsWithArrayAdapter extends ArrayAdapter<String> implements Filterable {

    private final List<String> mList = new ArrayList<>();
    private List<String> mFilteredList = new ArrayList<>();

    public StartsWithArrayAdapter(Context context, int textViewResourceId, ArrayList<String> list) {
        super(context, textViewResourceId, list);
        this.mList.addAll(list);
        this.mFilteredList.addAll(list);
    }

    @Override
    public int getCount() {
        return mFilteredList.size();
    }

    @NonNull
    @Override
    public Filter getFilter() {
        return new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence charSequence) {
                String charString = charSequence.toString();
                if (charString.isEmpty()) {
                    mFilteredList = mList;
                } else {
                    List<String> filteredList = new ArrayList<>();
                    for (String listItem : mList) {
                        if (listItem.toLowerCase().startsWith(charString.toLowerCase())) {
                            filteredList.add(listItem);
                        }
                    }
                    mFilteredList = filteredList;
                }

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

            @Override
            @SuppressWarnings("unchecked")
            protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
                mFilteredList = (ArrayList<String>) filterResults.values;
                clear();
                addAll(mFilteredList);
                notifyDataSetChanged();
            }
        };

    }
    
}

您代码中的用法:

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {


    StartsWithArrayAdapter adapterListOfWord;
    
    // rest of code

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        // omitted code
        
        adapterListOfWord = new StartsWithArrayAdapter(this, android.R.layout.simple_list_item_1, mSource);
        
        // rest of code

    }
}