如何在“编辑文本”和“列表视图”中添加搜索类型功能

时间:2014-07-07 12:34:54

标签: android search custom-lists

我有一个列表视图和一个编辑文本。当我写在编辑文本时,我想显示我的列表视图从这些字母开始。我正在为我的列表视图添加自定义适配器。

这是我的编辑文本addTextChangedListener

getFriendsName.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            // TODO Auto-generated method stub
            friends_available.setVisibility(View.VISIBLE);
            adapter.getFilter().filter(s);
        }

        @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

        }
    });

这是我的List视图,我将在其中设置自定义适配器

  adapter = new GroupAvailableFriendsToAddAdapter((BaseActivity) getActivity(),
                connectionsList);
        friends_available.setAdapter(adapter);

这是我的Adapter类

public class GroupAvailableFriendsToAddAdapter extends ArrayAdapter<ItemsAllPost> {

private BaseActivity _context;
private ArrayList<ItemsAllPost> data;
private LayoutInflater inflater = null;

private ItemsAllPost postData;
private String result;
@SuppressWarnings("unused")
private int count;
private TextView textView;
private ImageLoader imageLoader;



public GroupAvailableFriendsToAddAdapter(BaseActivity activity,
        ArrayList<ItemsAllPost> connectionsList) {
    super(activity, R.layout.group_avialable_friends_list_adapter, connectionsList);
    _context = activity;
    data = connectionsList;
    count=connectionsList.size();
    inflater = (LayoutInflater) _context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    imageLoader = new ImageLoader(activity);

}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = convertView;
    ItemsAllPost items = data.get(position);
    if (convertView == null) {
        view = inflater.inflate(R.layout.group_avialable_friends_list_adapter,
                null);
    }
    TextView FriendsName = (TextView) view
            .findViewById(R.id.group_available_friendNameTV);
    CircularImageView image_profile = (CircularImageView) view
            .findViewById(R.id.group_available_friendImgCI);
    FriendsName.setText(items.user_first_name);
    imageLoader.DisplayImage(items.profile_image,
            image_profile);


    return view;
}

}

问题是最初列表视图显示列表,但是当我输入文本时,它甚至隐藏了输入的文本与列表视图项目匹配,当我从编辑文本列表视图中删除文本时显示回来。

我做错了什么?

任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

我已经尝试了以下代码,它按预期为我工作:

@Override
    public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
           int textlength = cs.length();
           ArrayList<ContactStock> tempArrayList = new ArrayList<ContactStock>();
           for(ContactStock c: arraylist){
              if (textlength <= c.getName().length()) {
                 if (c.getName().toLowerCase().contains(cs.toString().toLowerCase())) {
                    tempArrayList.add(c);
                 }
              }
           }
           mAdapter = new ContactListAdapter(activity, tempArrayList);
           lv.setAdapter(mAdapter);
     }

您可以关注this tutorial以获取更多详细信息以及示例代码。

答案 1 :(得分:1)

阿里的方法要求您每次都使用新的搜索文本设置适配器。相反,您可以实现一个Filterable类,并在每次搜索后通知适配器有关数据更改的信息。将适配器更改为以下内容。它应该工作正常!

public class GroupAvailableFriendsToAddAdapter extends ArrayAdapter<ItemsAllPost> implements Filterable{
//create a new temporary list
private ArrayList<ItemsAllPost> data, tempListData;

public GroupAvailableFriendsToAddAdapter(BaseActivity activity, ArrayList<ItemsAllPost> connectionsList) {
    super(activity, R.layout.group_avialable_friends_list_adapter, connectionsList);
    data = connectionsList;
    **tempListData** = connectionsList; //make this list point to your original list
}

@Override
    public int getCount() {
        return **tempListData** .size(); // make the count point to this tempList as the list size would vary with every search text
    }

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ItemsAllPost items = **tempListData** .get(position); // reference data using this tempList
    FriendsName.setText(items.user_first_name);
    return view;
}

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

            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                FilterResults results = new FilterResults();
                final List<ItemsAllPost> tempFliteredDataList = new ArrayList<ItemsAllPost();
                // We implement here the filter logic
                if (constraint == null || constraint.toString().trim().length() == 0) {
                    // No filter implemented we return all the list
                    results.values = data;
                } else {
                    // We perform filtering operation
                    String constrainString = constraint.toString().toLowerCase();
                    for (ItemsAllPost post: data) {
                        if (post.user_first_name.toLowerCase().contains(constrainString)) {
                            tempFliteredDataList.add(post);
                        }
                    }
                    results.values = tempFliteredDataList ;
                }
                return results;

            }

            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {
                if (results.values!=null){
                    **tempListData** = (ArrayList<ItemsAllPost>) results.values; // returns the filtered list based on the search
                    notifyDataSetChanged();
                }
            }
        };
        return filter;
    }
}