在edittext上搜索循环视图

时间:2018-03-26 06:40:14

标签: android search android-recyclerview

我使用编辑文本创建了搜索视图,但它无效。我的编辑文本在对话框片段中。我试图将我的代码移动到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) {

        }
    });


}

1 个答案:

答案 0 :(得分:0)

我就像这样解决我的问题。首先我宣布我的模型

class LabelRenderer implements TableCellRenderer{

            @Override
            public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
                    int row, int column) {

                return (Component) value;
            }}
        table = new JTable();
        table.getColumn("image").setCellRenderer(new LabelRenderer());
        table.setColumnSelectionAllowed(true);
        table.setCellSelectionEnabled(true);
        scrollPane.setColumnHeaderView(table);

        scrollPane.setViewportView(table);


        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con=(Connection) DriverManager.getConnection("jdbc:mysql://192.168.1.180:3306/cardemo","root","sqladmin");
            PreparedStatement ps=con.prepareStatement("select  * from cardemo.imagetest");
            ResultSet rs=(ResultSet) ps.executeQuery();
            table.setModel(DbUtils.resultSetToTableModel(rs));

            label = new JLabel("");
            label.setBounds(464, 24, 122, 155);
            contentPane.add(label);

然后在循环视图中添加此

 public ArrayList<KodePos> data;

 public ArrayList<KodePos> tempData;

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

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

并且工作正常:)