我在android中创建了一个ListView,我想在列表上方添加EditText,当用户输入文本时,列表将根据用户输入进行过滤
有人可以告诉我,如果有办法过滤android中的列表适配器(使用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;
}
}
答案 0 :(得分:0)
首先,您需要在编辑文字上添加addTextChangeListener
。
然后在方法OnTextChanged
上编写用于过滤列表的代码。
这是我使用ArrayList的ListView过滤代码。
et_search是我的EditText,arrayList包含我要过滤的所有数据,templist将包含过滤的项目
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);
答案 1 :(得分:0)
这很简单,这对我有用
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) {
}
});