【问题标题】:Search on edittext for recycle view在edittext上搜索recyclerview
【发布时间】:2018-03-26 06:40:14
【问题描述】:

我已经使用编辑文本创建了搜索视图,但它不起作用。我的编辑文本在对话框片段中。我试图将我的代码移动到 onCreate() 但随后我的应用程序被强制关闭。编辑文本应搜索回收视图项。

它应该是自动搜索而不点击任何按钮。

这是我的搜索代码。

@SuppressLint("ResourceType")
@OnClick(R.id.button_choose)
void chooseLOV() {

    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
    LayoutInflater inflater = this.getLayoutInflater();
    View dialogView = inflater.inflate(R.layout.lov_kodepos, null);
    dialogBuilder.setView(dialogView);

    final RecyclerView recyclerView = (RecyclerView) dialogView.findViewById(R.id.rv_lov_kodepos);
    final EditText search  = (EditText) dialogView.findViewById(R.id.editText_lov_search);

    API.getKodePos().enqueue(new Callback<ArrayList<KodePos>>() {
        @Override
        public void onResponse(Call<ArrayList<KodePos>> call, Response<ArrayList<KodePos>> response) {
            if (response.code()== 200){
                Log.i("bella", "onResponse: "+response);

                data = response.body();
                recyclerView.setHasFixedSize(true);
                recyclerView.addItemDecoration(new DividerItemDecoration(AddCustomerActivity.this, DividerItemDecoration.VERTICAL));
                recyclerView.setLayoutManager(new LinearLayoutManager(AddCustomerActivity.this));
                recyclerView.setAdapter(new KodePosAdapter(data));
            }
        }

        @Override
        public void onFailure(Call<ArrayList<KodePos>> call, Throwable t) {
            Toast.makeText(AddCustomerActivity.this, "Failed", Toast.LENGTH_SHORT).show();

        }
    });

    final AlertDialog alertDialog = dialogBuilder.create();
    alertDialog.show();

    recyclerView.addOnItemTouchListener(new RecyclerItemClickListener(AddCustomerActivity.this, new RecyclerItemClickListener.OnItemClickListener() {
        @Override
        public void onItemClick(View view, int position) {
            et_kodepos.setText(data.get(position).getPosKode());
            et_kota.setText(data.get(position).getPosKota());
            et_provinsi.setText(data.get(position).getPosProp());
            et_kecamatan.setText(data.get(position).getPosCamat());
            et_kelurahan.setText(data.get(position).getPosLurah());

            alertDialog.dismiss();
        }
    }));

    search.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) {
            ArrayList<KodePos> kodePos = new ArrayList<>();
            if(tempDatass!=null){
                for (KodePos kodePos1:data){p
                    String dat = s.toString().toLowerCase();
                    if (kodePos1.getPosCamat().toLowerCase().contains(dat)){
                        kodePos.add(kodePos1);
                    }
                }
                tempDatass = kodePos;
                recyclerView.setAdapter(new KodePosAdapter(tempDatass));
            }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });


}

【问题讨论】:

  • 什么是tempDatass?你确定它在onTextChanged() 中不为空吗?无论如何,看起来你根本不需要它。
  • 它是一个数组列表模型@MikeM,那么我应该删除它吗?
  • 好吧,不管它是什么开始,它都不会在那里使用,因为你丢弃它,并将它设置为kodePos。也就是说,这有点毫无意义。
  • 我已将其删除。仍然不适合我
  • 哦,是的,看起来这就是你试图用tempDatass 做的事情。在onTextChanged() 中,您需要保留对过滤后的ArrayList 的引用,并在onItemClick() 中调用get(position),而不是data。所以回到你原来的样子,只是删除if(tempDatass!=null),并将onItemClick()中的所有data.get(position)调用替换为tempDatass.get(position)。对于那个很抱歉。我没看到你想在那里做什么。

标签: android search android-recyclerview


【解决方案1】:

我只是这样解决我的问题。首先我声明我的模型

 public ArrayList<KodePos> data;

 public ArrayList<KodePos> tempData;

然后在回收视图中添加这个

 data = response.body();
 tempData = data;

之后我的文本改变了我这样的代码

 search.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) {
            ArrayList<KodePos> kodePos = new ArrayList<>();
            if (tempData!=null){
                for (KodePos kodePosSatu: data ){
                    String dat = s.toString().toLowerCase();
                    if (kodePosSatu.getPosCamat().toLowerCase().contains(dat)) {
                        kodePos.add(kodePosSatu);
                    }
                }
                tempData = kodePos;
                recyclerView.setAdapter(new KodePosAdapter(tempData));
            }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });

而且效果很好:)

【讨论】:

    猜你喜欢
    • 2021-12-03
    • 2020-08-23
    • 1970-01-01
    • 2017-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-01
    相关资源
    最近更新 更多