我是android新手。我想要自定义ListView
的搜索功能。
我正在处理项目,我尝试使用listView
过滤addOnTextChangedListener
我在网上看到我的情况并没有找到任何帮助。
这是我的代码。
EmployeeFragment.java
public class EmployeeFragment extends Fragment {
private EditText searchEditText1;
EmployeeDatabaseHelper dbHelper;
ListView employeeList;
public Cursor employees;
private ArrayList<Employee> arrlst;
private myadapter myadapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_employee, container, false);
searchEditText1 = (EditText) rootView.findViewById(R.id.searchEditText1);
employeeList = (ListView) rootView.findViewById(R.id.employeeList);
searchEditText1.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
ArrayList<Employee> tempList = new ArrayList<Employee>();
for(Employee emp : arrlst){
if(emp.name.contains(s) || emp.username.contains(s))
tempList.add(emp);
}
//I am stucked here...
//What to do here
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
return rootView;
}
@Override
public void onResume() {
populateEmployeeList();
super.onResume();
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
populateEmployeeList();
}
public void populateEmployeeList(){
dbHelper=new EmployeeDatabaseHelper(getActivity());
arrlst = dbHelper.getAllEmployee();
myadapter = new myadapter(getActivity());
employeeList.setAdapter(myadapter);
}
class myadapter extends ArrayAdapter<Employee> {
Activity con;
private LayoutInflater Inflater;
public myadapter(Activity context) {
super(context,R.layout.row);
con=context;
Inflater = (LayoutInflater)con.getSystemService(con.LAYOUT_INFLATER_SERVICE);
}
class ViewHolder{
TextView tvid;
TextView tvname;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View vi = convertView;
ViewHolder vh;
if(vi==null)
{
vh = new ViewHolder();
vi= Inflater.inflate(R.layout.row, null);
vh.tvid=(TextView)vi.findViewById(R.id.mainTextView);
vh.tvname=(TextView)vi.findViewById(R.id.subTextView);
vi.setTag(vh);
}else{
vh=(ViewHolder)vi.getTag();
}
Employee e =arrlst.get(position);
vh.tvid.setText(e.name);
vh.tvname.setText(e.username);
return vi;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return arrlst.size();
}
}
}
和员工类
Employee.java
public class Employee {
public String username;
public String name;
public String password;
}
答案 0 :(得分:0)
试试这个,一旦你将你的元素放在临时列表中,你需要等同于tempr中的arrlst并重新初始化适配器。 - &GT;
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
ArrayList<Employee> tempList = new ArrayList<Employee>();
for(Employee emp : arrlst){
if(emp.name.contains(s) || emp.username.contains(s))
tempList.add(emp);
}
//I am stucked here...
//What to do here
arrlst = tempList;
myadapter = new myadapter(getActivity());
employeeList.setAdapter(myadapter);
}
答案 1 :(得分:0)
您的适配器必须implements Filterable
然后致电
myFilter.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
dataAdapter.getFilter().filter(s.toString());
}
});
有关详细信息,请访问此link