简体   繁体   中英

ListView Filter Android Using SimpleAdapter

I have created a ListView in android and I want to add EditText above the list and when the user enter text the list will be filtered according to user input

Can anyone tell me please if there is a way to filter the list adapter in android (Use SimpleAdapter) ?

public class DanhSachHopDongChoNghiemThuActivity extends AppCompatActivity implements TextWatcher {
    String username = ...
    String password = ...
    String database = ...
    String server = ...
    Connection connect;
    EditText inputSearch;
    ListView listView;
    SimpleAdapter simpleAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        inputSearch = (EditText)findViewById(R.id.editString);
        listView = (ListView)findViewById(R.id.listViewDanhSachHopDongChoNghiemThu);
        connect = CONN(username, password, database, server);
        querySQL();
        editString.addTextChangedListener(this);
    }
    public void querySQL() {
        ResultSet rs;
        try{
            PreparedStatement statement =
                    connect.prepareStatement("T_APIgetListThueBaoChoNghiemThu " + MainActivity.userId);
            rs = statement.executeQuery();
            List<Map<String,String>> data = null;
            data = new ArrayList<Map<String,String>>();
            if (rs != null) {
                while (rs.next()) {
                    Map<String, String> datanum = new HashMap<String, String>();
                    datanum.put("NgayHD", "Ngày HĐ: " + rs.getString("NgayLapHD"));
                    datanum.put("TenTB", rs.getString("TenTB"));
                    datanum.put("MaTB", rs.getString("MaTB"));
                    datanum.put("DiaChi", rs.getString("DiaChi"));
                    datanum.put("DaiVT", rs.getString("DaiVTTen"));
                    datanum.put("GhiChu", "Ghi chú: " + rs.getString("GhiChu"));
                    datanum.put("TenDVVT", rs.getString("DVVTTen"));
                    datanum.put("TrangThai", rs.getString("TrangThai"));
                    data.add(datanum);

                    String[] from = {"TenDVVT", "NgayHD", "TenTB", "MaTB", "DaiVT", "DiaChi", "GhiChu", "TrangThai"};
                    int[] to = {R.id.DichVu, R.id.txtNgayHD, R.id.txtTenTB, R.id.txtMaTB, R.id.txtDaiVT, R.id.txtDiaChi, R.id.txtGhiChu, R.id.txtTrangThai};

                    simpleAdapter = new SimpleAdapter(this, data, R.layout.danhsachchonghiemthu_listview_layout, from, to);
                    listView.setAdapter(simpleAdapter);
                }
            }
        }catch(SQLException e){
            e.printStackTrace();
        }
    }
    @SuppressLint("NewApi")
    private Connection CONN(String _user, String _pass, String _DB,
                            String _server) {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                .permitAll().build();
        StrictMode.setThreadPolicy(policy);
        Connection conn = null;
        String ConnURL = null;
        try {
            Class.forName("net.sourceforge.jtds.jdbc.Driver");
            ConnURL = "jdbc:jtds:sqlserver://" + _server + ";"
                    + "databaseName=" + _DB + ";user=" + _user + ";password="
                    + _pass + ";";
            conn = DriverManager.getConnection(ConnURL);
        } catch (SQLException se) {
            Log.e("ERRO", se.getMessage());
        } catch (ClassNotFoundException e) {
            Log.e("ERRO", e.getMessage());
        } catch (Exception e) {
            Log.e("ERRO", e.getMessage());
        }
        return conn;
    }
}

First of all you need to add addTextChangeListener on your edittext.

Then on Method OnTextChanged write the code in which your filter your list.

Here is my Code of ListView Filtering using ArrayList.

et_search is my EditText, arrayList contains all my data to filter, templist will contain the filtered items

ArrayList templist = new ArrayList<GroupItemsData>();
                String searchString = et_search.getText().toString();
                for (int i = 0; i < arrayList.size(); i++) {
                    String currentString = arrayList.get(i).getGroupTitle().toLowerCase(Locale.getDefault());
                    if (currentString.contains(searchString)) {
                        Log.e("Test Test", arrayList.get(i).getGroupTitle());
                        templist.add(arrayList.get(i));
                    }
                }
                adapter = new DiscountListViewAdapter(mContext, templist, activity);  
               listview.setAdapter(mAdapter);

it is very simple and this is work for me

editText_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) {
            ((SimpleAdapter)All_Reports_Fragment.this.listAdapter).getFilter().filter(s);
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM