使用EditText过滤ListView

时间:2014-10-30 12:12:53

标签: java android

基本上我尝试使用EditText过滤ListView。我设法存储所有过滤的结果,但似乎我错过了一些东西,因为UI没有更新,并且它没有显示任何文本更改。有什么迹象吗?谢谢

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.TextView;

import com.enloop.R;

import java.util.ArrayList;

public class FriendsWithMeAdapter extends BaseAdapter implements Filterable {
    private Context mContext;
    private ArrayList<FriendsModel> friend;
    private ArrayList<FriendsModel> items = new ArrayList<FriendsModel>();
    private ArrayList<FriendsModel> suggestions = new ArrayList<FriendsModel>();

    public FriendsWithMeAdapter(Context c, ArrayList<FriendsModel> ai) {
        mContext = c;
        friend = ai;
    }

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

    @Override
    public Object getItem(int position) {
        return friend.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater mInflater = (LayoutInflater)
                    mContext.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            convertView = mInflater.inflate(R.layout.item_friend, null);
        }
        ImageView userImage = (ImageView) convertView.findViewById(R.id.invited_friend_picture);
        ImageView checkImage = (ImageView) convertView.findViewById(R.id.invite_friends_check);
        TextView name = (TextView) convertView.findViewById(R.id.invited_friend_name);
        TextView userName = (TextView) convertView.findViewById(R.id.invited_friend_name);

        userImage.setImageBitmap(friend.get(position).getUserImage());
        checkImage.setImageResource(friend.get(position).isWithMe() ?
                R.drawable.btn_checkmark_round_tiny : R.drawable.btn_before_checkmark_round_tiny);

        String userEnloopNameString = friend.get(position).getName();
        name.setText(userNameString);


        String userNameString = friend.get(position).getUserName();
        if (userNameString.length() > 12) {
            userNameString = userNameString.substring(0, 10);
            userNameString += "..";
        }
        userName.setText(userNameString);

        return convertView;
    }

    @Override
    public Filter getFilter() {
        Filter filter = new Filter() {

            @Override
            protected FilterResults performFiltering(CharSequence constraint) {

                FilterResults results = new FilterResults();

                if (constraint == null && constraint.length() == 0) {
                    results.values = friend;
                    results.count = friend.size();
                } else {
                    for (FriendsModel item : friend) {
                        if (item.getName().toLowerCase().contains(constraint.toString().toLowerCase()))
                            suggestions.add(item);
                    }
                    results.values = suggestions;
                    results.count = suggestions.size();
                }

                return results;
            }


            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {

                if (results.count == 0) {
                    notifyDataSetInvalidated();
                } else {
                    items = (ArrayList<FriendsModel>) results.values;
                }
            }
        };
        return filter;
    }
}




mSearch.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) {
                adapter.getFilter().filter(s.toString());
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

1 个答案:

答案 0 :(得分:1)

请尝试这种方式,希望这有助于您解决问题。

mSearch.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) {

      }

      @Override
      public void afterTextChanged(Editable s) {
           adapter.filter(text);
      }
});

这里使用两个列表一个用于原始数据,另一个用于过滤数据请记住你不能使用(=)分配过滤数据如果你这样做那么你过滤数据也会改变原始数据,过滤数据使用到显示列表列表项,因此您必须使用addAll()而不是(=)赋值,这不是原始数据的更改,每当您使用自定义适配器时尝试使用ViewHolder模式。

public class FriendsWithMeAdapter extends BaseAdapter implements Filterable {
    private Context mContext;
    private ArrayList<FriendsModel> friend;
    private ArrayList<FriendsModel> suggestions = new ArrayList<FriendsModel>();

    public FriendsWithMeAdapter(Context mContext, ArrayList<FriendsModel> friend) {
        this.mContext = mContext;
        this.friend = friend;
        suggestions =new ArrayList<FriendsModel>();
        suggestions.addAll(friend);
    }

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

    @Override
    public Object getItem(int position) {
        return suggestions.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView =  LayoutInflater.from(mContext).inflate(R.layout.item_friend, null);
            holder.userImage = (ImageView) convertView.findViewById(R.id.invited_friend_picture);
            holder.checkImage = (ImageView) convertView.findViewById(R.id.invite_friends_check);
            holder.name = (TextView) convertView.findViewById(R.id.invited_friend_name);
            holder.userName = (TextView) convertView.findViewById(R.id.invited_friend_name);

            convertView.setTag(holder);

        }else{
            holder = (ViewHolder) convertView.getTag();
        }


        holder.userImage.setImageBitmap(suggestions.get(position).getUserImage());
        holder.checkImage.setImageResource(suggestions.get(position).isWithMe() ?
                R.drawable.btn_checkmark_round_tiny : R.drawable.btn_before_checkmark_round_tiny);

        String userEnloopNameString = suggestions.get(position).getName();
        holder.name.setText(userNameString);


        String userNameString = suggestions.get(position).getUserName();
        if (userNameString.length() > 12) {
            userNameString = userNameString.substring(0, 10);
            userNameString += "..";
        }
        holder.userName.setText(userNameString);

        return convertView;
    }

    public void filter(String charText) {
        suggestions.clear();
        if (charText.length() == 0) {
            suggestions.addAll(friend);
        }else{
            for (FriendsModel item : friend) {
                if (item.getName().toLowerCase().contains(constraint.toString().toLowerCase()))
                    suggestions.add(item);
            }
        }
        notifyDataSetChanged();
    }


    public class ViewHolder {
        ImageView userImage;
        ImageView checkImage;
        TextView name;
        TextView userName;
    }
}