在Android中的Edittext上键入内容时,如何在listview上使用过滤器

时间:2016-06-23 04:00:04

标签: java android listview mobile android-edittext

我想通过edittext找出指定用户。但是edittext是ListView的第一行。 我怎样才能做到这一点?

 public class ChatFragment extends Fragment {
    private ListView chatListView;
    private List<ChatItem> items;
    private ChatAdapter chatAdapter;

    public static ChatFragment newInstance(){
        ChatFragment fragment = new ChatFragment();
        return fragment;
    }

    @Override public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);

    }

    @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saveInstanceState){
        View view = inflater.inflate(R.layout.chat_fragment, container, false);
        chatListView = (ListView)view.findViewById(R.id.chatListView);
        setupChatList();
        chatListView.setTextFilterEnabled(true);
        return view;
    }

    public void setupChatList(){
        items = new ArrayList<>();
        items.add(new ChatItem(1, "","","",""));
        items.add(new ChatItem(2 ,"layer1", "Chloe Grace Moretz", "Approved your see post request!", "4 mins"));
        items.add(new ChatItem(3 ,"layer2", "Robert Downey", "ok!", "16:19"));
        items.add(new ChatItem(4 ,"layer3", "Amanda Crew", "haha", "19:01"));
        items.add(new ChatItem(5 ,"layer4", "Noah Urrea", "Good choice ~ man", "Yesterday"));
        items.add(new ChatItem(6 ,"layer5", "Blake Rayne", "Jason Papadopoulos I think", "Sunday"));
        chatAdapter = new ChatAdapter(getContext(), items);
        chatListView.setAdapter(chatAdapter);
    }

    public void remove(){
        chatAdapter.remove(items.get(1));
    }
}

我的适配器如下:

  public class ChatAdapter extends ArrayAdapter<ChatItem> {
    private List<ChatItem> items;
    private List<ChatItem> filteredData = null;
    private TextView chatNameTxt;
    private TextView msgTxt;
    private TextView msgTimeTxt;
    private ImageView chatImg;
    private ImageView chatMoreImg;
    private Context context;
    private ItemFilter filter = new ItemFilter();
    private EditText findChat;

    public ChatAdapter(Context context, List<ChatItem> items){
        super(context, -1, items);
        this.items = items;
        this.context = context;
    }

    @Override public View getView(int position, View convertView, ViewGroup parent){
        LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (position==0){
            View item0 = inflater.inflate(R.layout.chat_list_view_item0, parent, false);
            findChat = (EditText)item0.findViewById(R.id.findChat);
            findChat.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) {
                    ChatFragment chatFragment = new ChatFragment();
                    chatFragment.remove();
                }
            });
            return item0;
        }else {
            View item = inflater.inflate(R.layout.chat_list_view, parent, false);
            chatNameTxt = (TextView) item.findViewById(R.id.chatNameTxt);
            msgTxt = (TextView) item.findViewById(R.id.msgTxt);
            msgTimeTxt = (TextView) item.findViewById(R.id.msgTimeTxt);
            chatImg = (ImageView)item.findViewById(R.id.chatImg);
            chatMoreImg = (ImageView)item.findViewById(R.id.chatMoreImg);

            ChatItem friendItem = items.get(position);
            String chatName = friendItem.getUserNmae();
            String msg = friendItem.getLastMsg();
            String msgTime = friendItem.getLastMsgTime();
            String userImg = friendItem.getUserImageName();

            chatImg.setImageResource(context.getResources().getIdentifier(userImg, "drawable", context.getPackageName()));
            chatNameTxt.setText(chatName);
            msgTxt.setText(msg);
            msgTimeTxt.setText(msgTime);

            return item;
        }
    }
}

3 个答案:

答案 0 :(得分:1)

Filter上输入ListView EditText时,您已按照以下方式操作。

  1. TextWatcher添加到EditText#addTextChangedListener
  2. onTextChanged中添加或删除ListView适配器中的项目。如果您是ArrayAdapter的子类,则会有addremove方法
  3. 如果您是BaseAdapter的子类,请创建BaseAdapter implements Filterable
  4.   

    注意:第2步:适用于ArrayAdapter,第3步适用于   BaseAdapter

答案 1 :(得分:0)

您可以使适配器 Filterable 。它会自动更新listview的值。

我明天会在这里写教程,并保留链接以表彰作者。

答案 2 :(得分:0)

无需创建额外的过滤器类。您只需保留TextWatcher的字符串,然后使用contains方法

将其与列表视图数据一起检查
searchforcategory.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            getAllCetegory(charSequence.toString());

        }

        @Override
        public void afterTextChanged(Editable editable) {



        }
    });

现在只需检查数据中的字符串

 for(int i=0;i<response.body().getDataList().size();i++){

                    if(response.body().getDataList().get(i).getName().contains(checkString)){


                        productDataLists.add(response.body().getDataList().get(i));

                    }


                }