无法在 android 中筛选自定义列表视图

Unable to filter custom List View in android

我是 android 的新手。我想要自定义 ListView 的搜索功能。 我正在做一个项目,我试图使用 addOnTextChangedListener 过滤我的 listView 我在网上看到我没有发现任何对我的情况有帮助的东西。

这是我的代码。

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();
}
}   
}

和员工 class

Employee.java

public class Employee {
    public String username;
    public String name;
    public String password;
}

试试这个,将元素放入模板列表后,您需要等于模板列表中的 arrlst 并重新初始化适配器。 ->

   @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); 



        }

您必须 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