ListView的Android Filter arrayAdapter不起作用

时间:2016-06-23 10:19:14

标签: android listview android-edittext

你好我有一个edittext并希望如果我输入Listview将是filtered并且只显示输入的值,如果它在列表中,我尝试了许多方法但没有任何效果

2 个答案:

答案 0 :(得分:0)

通过以下代码更改您的适配器:

   public class CustomListviewAdapter extends ArrayAdapter<Personne> implements Filterable {

       private LayoutInflater inflater;
       private ArrayList<Personne> data;
       private ArrayList<Personne> filteredData = null;
       private Activity mContext;
       private int layoutResourceId;

       public CustomListviewAdapter(Activity context, int resource, ArrayList<Personne> objects) {

           super(context, resource, objects);
           data = objects;
           filteredData = objects;
           mContext = context;
           layoutResourceId = resource;

       }

       @Override
       public int getCount() {
           return filteredData.size();
       }

       @Override
       public Personne getItem(int position) {
           return super.getItem(position);
       }

       @Override
       public int getPosition(Personne item) {
           return super.getPosition(item);
       }

       @Override
       public long getItemId(int position) {
           return super.getItemId(position);
       }

       @Override
       public View getView(final int position, View convertView, ViewGroup parent) {

           View row = convertView;
           ViewHolder viewHolder = null;

           if (row == null) {

               inflater = LayoutInflater.from(mContext);
               row = inflater.inflate(layoutResourceId, parent, false);

               viewHolder = new ViewHolder();

               viewHolder.imageView = (ImageView) row.findViewById(R.id.icon);
               viewHolder.nom = (TextView) row.findViewById(R.id.nameLabel);
               viewHolder.pseudo = (TextView) row.findViewById(R.id.usernameLabel);
               viewHolder.email = (TextView) row.findViewById(R.id.emailLabel);

               row.setTag(viewHolder);

           }
           else {

               viewHolder = (ViewHolder) row.getTag();

           }

           viewHolder.personne = filteredData.get(position);

           viewHolder.nom.setText("Nom: " + viewHolder.personne.getNom());
           viewHolder.pseudo.setText("Pseudo: "  + viewHolder.personne.getUsername());
           viewHolder.email.setText("Email: "  +            viewHolder.personne.getEmail());

           return row;

       }




Filter filter = new Filter() {
  @Override
  protected FilterResults performFiltering(CharSequence constraint) {
       String filterString = constraint.toString().toLowerCase();
       FilterResults results = new FilterResults();
       final List<String> tempList = data ;
       int count = tempList.size();
       final ArrayList<String> nlist = new ArrayList<String>(count);
       String filterableString ;
       for (int i = 0; i < count; i++) {
            filterableString = tempList.get(i).getUserName();
            if (filterableString.toLowerCase().contains(filterString)) {
                nlist.add(filterableString);
             }
       }
       results.values = nlist;
       results.count = nlist.size();
       return results;
   }

  @SuppressWarnings("unchecked")
  @Override
  protected void publishResults(CharSequence constraint, FilterResults results) {
       filteredData = (ArrayList<String>) results.values;
       notifyDataSetChanged();
       }
}



       public class ViewHolder {

           private Personne personne;
           private TextView nom;
           private TextView pseudo;
           private TextView email;

       }
   }

答案 1 :(得分:0)

将适配器代码更改为以下代码:

public class CustomListviewAdapter extends ArrayAdapter<Personne> {

private LayoutInflater inflater;
private ArrayList<Personne> data;
private ArrayList<Personne> tempList;
private Activity mContext;
private int layoutResourceId;

public CustomListviewAdapter(Activity context, int resource, ArrayList<Personne> objects) {
    data = objects;
    tempList = new ArrayList<>();
    tempList.addAll(data);
    mContext = context;
    layoutResourceId = resource;
    super(context, resource, tempList);        
}

@Override
public int getCount() {
    return tempList.size();
}

@Override
public Personne getItem(int position) {
    return super.getItem(position);
}

@Override
public int getPosition(Personne item) {
    return super.getPosition(item);
}

@Override
public long getItemId(int position) {
    return super.getItemId(position);
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    View row = convertView;
    ViewHolder viewHolder = null;

    if (row == null) {

        inflater = LayoutInflater.from(mContext);
        row = inflater.inflate(layoutResourceId, parent, false);

        viewHolder = new ViewHolder();

        viewHolder.imageView = (ImageView) row.findViewById(R.id.icon);
        viewHolder.nom = (TextView) row.findViewById(R.id.nameLabel);
        viewHolder.pseudo = (TextView) row.findViewById(R.id.usernameLabel);
        viewHolder.email = (TextView) row.findViewById(R.id.emailLabel);

        row.setTag(viewHolder);

    }
    else {

        viewHolder = (ViewHolder) row.getTag();

    }

    viewHolder.personne = tempList.get(position);

    viewHolder.nom.setText("Nom: " + viewHolder.personne.getNom());
    viewHolder.pseudo.setText("Pseudo: "  + viewHolder.personne.getUsername());
    viewHolder.email.setText("Email: "  + viewHolder.personne.getEmail());
    return row;

}

public void filter(String text) {
    if(text != null && data!=null) {
        int length=data.size();
        int i=0;
        tempList.clear();
        if(text.isEmpty()){
            tempList.addAll(data);
        } else{
            while(i<length){
                Personne item=data.get(i);
                String username = item.getUsername();
                if (username.toLowerCase().startsWith(text.toLowerCase())) {
                    tempList.add(item);
                }
                i++;
            }
        }
        notifyDataSetChanged();
    }
}

public class ViewHolder {

    private Personne personne;
    private TextView nom;
    private TextView pseudo;
    private TextView email;

}

}

基本上我正在尝试维护一个主要的数据列表和临时列表,这些列表将用于过滤从主列表中获取的数据。以上代码基于username进行过滤,如果您希望添加其他参数进行搜索,则可以在过滤方法的if循环内添加/编辑while条件。