搜索功能在android中无法正常工作

时间:2020-04-07 06:59:15

标签: android search android-recyclerview

您好,在下面的代码中,我正在实现搜索功能。在Api中,我正在出租帐户名称。对于“实现列表”,我正在使用recyclerView。

在实现此功能时,我遇到了一个问题。在ui部分中,我有一个edittext和recyclerview。

当我搜索“ a”时,当我尝试选择该项目时,它会显示列表,即选择另一个文本,然后将另一个文本设置为edittext。

我在哪里犯了错误,任何人都可以帮助我

searchAccount.addTextChangedListener(new TextWatcher() {
                                        @Override
                                        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                                        }

                                        @Override
                                        public void onTextChanged(final CharSequence charSequence, int i, int i1, int i2) {
                                            adapter = new CustomAdapter(account_name);
                                            recyclerViewAccount.setHasFixedSize(true);
                                            recyclerViewAccount.setLayoutManager(new LinearLayoutManager(getContext()));
                                            recyclerViewAccount.setAdapter(adapter);
                                            // editTextSearch.setText("ABC");
                                            recyclerViewAccount.clearFocus();

                                            recyclerViewAccount.addOnItemTouchListener(new RecyclerTouchListener(getContext(), recyclerViewAccount, new RecyclerTouchListener.ClickListener() {

                                                @Override
                                                public void onClick(View view, int position) {


                                                    RecordsAccounts recordsAccounts=recordsListAccount.get(position);
                                                    account_id =recordsAccounts.getId();
                                                    accountnames =recordsAccounts.getAccountname();
                                                    //account_name=recordsAccounts.getAccountname().toString();
                                                    Log.e("account_id",account_id);
                                                    searchAccount.setText(accountnames);



                                                }

                                                @Override
                                                public void onLongClick(View view, int position) {

                                                }
                                            }));
                                        }

                                        @Override
                                        public void afterTextChanged(Editable editable) {
                                            //after the change calling the method and passing the search input
                                            filter(editable.toString());


                                        }
                                    });



private void filter(String text) {
        //new array list that will hold the filtered data
        ArrayList<String> filterdNames = new ArrayList<>();

        //looping through existing elements
        for (String s : account_name) {
            //if the existing elements contains the search input
            if (s.toLowerCase().contains(text.toLowerCase())) {
                //adding the element to filtered list
                filterdNames.add(s);
                //  editTextSearch.setText(text);
            }
            // editTextSearch.setText(s);
        }

        //calling a method of the adapter class and passing the filtered list


        adapter.filterList(filterdNames);
    }

0 个答案:

没有答案