如何在没有searchview的情况下在片段中添加搜索功能

时间:2019-07-19 07:18:49

标签: android android-fragments

OneFragment

searchitem = v.findViewById(R.id.searchitem);
searchitem.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) {
        String text = searchitem.getText().toString().toLowerCase(Locale.getDefault());
            filter(text);
            Log.d("bfds", "" + text);
        }


    @Override
    public void afterTextChanged(Editable s) {}
});
onefragment和contactlist中的

filter方法是用于在列表中显示数据的数组列表

private void filter(String text) {
    //new array list that will hold the filtered data

    text = text.toLowerCase(Locale.getDefault());
    Log.d("ujhl",""+text);
    //looping through existing elements
    for (HashMap<String, String> s : contactList) {
        //if the existing elements contains the search input
        if (s.containsValue(text.toLowerCase())) {
            //adding the element to filtered list

            contactList.add(s);
            continents = populateContinentData(continents);
            adapter = new MyExpandableAdapter(getActivity(), continents);
            Log.d("TY","YTUTY"+continents);
        }
    }
}

2 个答案:

答案 0 :(得分:0)

尝试使用此代码:

private void filter(String text) {
    //new array list that will hold the filtered data
    HashMap<String, String> contactListFiltered = new HashMap<String, String>();
    text = text.toLowerCase(Locale.getDefault());
    Log.d("ujhl",""+text);
    //looping through existing elements
    for (HashMap<String, String> s : contactList) {
        //if the existing elements contains the search input
        if (s.containsValue(text.toLowerCase())) {
            //adding the element to filtered list
            contactListFiltered.putAll(s);
        }
    }
    ArrayList<MainProductName> continents = populateContinentData(contactListFiltered);
    adapter = new MyExpandableAdapter(getActivity(), continents);
    yourListView.setAdapter(adapter);
    Log.d("TY","YTUTY"+continents);
}

public ArrayList<MainProductName> populateContinentData(HashMap<String, String> contactList) {
    // Asia MainProductName

    ArrayList<MainProductName> products = new ArrayList();
     for (int i = 0; i < contactList.size(); i++) {
         MainProductName conti = new MainProductName(i, "" + contactList.get(i), null, R.drawable.moong);
         products.add(conti);
     }

     return products;
}

让我们知道结果

答案 1 :(得分:0)

您以错误的方式执行操作您无法使用 foreach 循环来迭代 HashMap ,并且您又在 EditText上调用getText()< / strong>(我想)是每当文本发生更改时。

String text = searchitem.getText().toString().toLowerCase(Locale.getDefault());

因此不需要这样做。您可以使用 CharSequence 将列表过滤为

searchitem.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) {
            filter(s); // get this **s** as queryText in filter method
            Log.d("bfds", "" + text);
        }


    @Override
    public void afterTextChanged(Editable s) {}
});

正如我之前所说,您不能将foreach用于哈希图,因此只能在两个基础上拟合哈希图

  1. 密钥库List<Stirng> keys= contactList.keySet();
  2. 价值基础List<Stirng> values= contactList.values();

    for (String s : keys/values) {
    // if list item contains the input string char sequence
    if (s.toLowerCase().containsValue(queryText.toLowerCase())) {
        //add to your fitler list and set to adapter here
    }
    
    //or you can use startsWith to check if list item string starts with input char sequence like
     if (s.toLowerCase().startsWith(queryText.toLowerCase())) {
        //add to fitlered list and set to adapter here
    }
    

    }