How to implement search on a multiline edit text i

2019-07-26 23:01发布

问题:

I have a edit text for searching customer names.It is a multi line edit text field.I want to give the end user the ability to add multiple users in a single edit text.

So the end user types in the first name selects user from the drop down and presses enter to add the new user. I am able to search for the first user but unable to search for the next user.

Following is the code that I am using:-

public TextWatcher searchTextWatcher = new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence query, int start, int before, int count) {
        mRecyclerView.setVisibility(View.VISIBLE);
        query = query.toString().toLowerCase();

        final List<String> filteredList = new ArrayList<>();

        for (int i = 0; i < list.size(); i++) {

            String text = list.get(i).toLowerCase();
            if (query.length()>0 && query.subSequence(query.length()-1, query.length()).toString().equalsIgnoreCase("\n"))
            {
                String multiLines = query.toString();
                String[] names;
                String delimiter ="\n";

                names = multiLines.split(delimiter);
                ArrayList guest_info = new ArrayList(Arrays.asList(names));

                ListIterator<String> it = guest_info.listIterator();
                while (it.hasNext())
                {

                    String guest = it.next();
                    Log.d("Guest ", guest);
                    if (text.toLowerCase().contains(guest))
                    {
                        filteredList.add(list.get(i));
                    }
                }




            }else
            if(text.contains(query))
            {
                filteredList.add(list.get(i));
            }
        }
        mRecyclerView.setLayoutManager(new LinearLayoutManager(BookingActivity.this));
        guestAdapter = new GuestAdapter(filteredList, getApplicationContext(),BookingActivity.this);
        mRecyclerView.setAdapter(guestAdapter);
        guestAdapter.notifyDataSetChanged();  // data set changed

    }

    @Override
    public void afterTextChanged(Editable editable) {
        String name = editable.toString();
        if (name.equals(""))
        {
            mRecyclerView.setVisibility(View.GONE);
        }

    }
};

I am new to programming so any help or suggestion is appreciated.Thank you.