【问题标题】:onTextChanged, cant get it to work rightonTextChanged,无法正常工作
【发布时间】:2013-07-05 03:08:53
【问题描述】:

大家好,我需要我的搜索功能来充当联系人。我正在从服务器检索我的参与者列表,并在用户搜索以获取结果时重建它。胡佛我只有在他们输入全名时才能让它工作。让我给你看我的代码。我尝试了很多组合。任何想法?这是儿童饥饿联盟的捐赠应用程序。 =]

这只是在一个 for 循环中添加每个名称 fyi

if (searchQ.contains(child.first_name.toLowerCase()) 
|| searchQ.matches(child.last_name.toLowerCase()) {

 id = child.id;
 addChildToList(child); 

 }

 if (searchQ.matches("")){
    addChildToList(child);


 }

这是我的文本观察器

public void searchChild(){
    final EditText ET = (EditText) findViewById(R.id.search);


    // create the TextWatcher
    TextWatcher textWatcher = 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) {



            // Rebuilds the list
            String searchQ = ET.getText().toString();
             getChildrenListSearch(searchQ);


        }

        @Override
        public void afterTextChanged(Editable editable) {
            // Remove rows that do no match
            TableLayout tl = (TableLayout)findViewById(R.id.childList); 
            tl.removeAllViews();


        }
    };

    //we must add the textWatcher to our EditText
    ET.addTextChangedListener(textWatcher);

    }

【问题讨论】:

    标签: java android search android-edittext


    【解决方案1】:

    反过来……

    if (searchQ.contains(child.first_name.toLowerCase()) 
        || searchQ.matches(child.last_name.toLowerCase()) {
    
         id = child.id;
         addChildToList(child); 
    
         }
    

    到这个...

    if (child.first_name.toLowerCase().contains(searchQ) 
            || searchQ.matches(child.last_name.toLowerCase()) {
    
             id = child.id;
             addChildToList(child); 
    
             }
    

    【讨论】:

    • 它会给你什么错误?你也明白为什么它需要这样吗?您正在查看搜索词是否与孩子姓名部分匹配,而不是相反
    • nvm 你的 cbode 工作得很好,只需要切换一些东西,我没有那样看!我现在明白了!有没有办法让它 equalIgnoreCase 并同时包含而无需我编写另一个函数?
    • 只使用 searchQ.toLowerCase()
    猜你喜欢
    • 1970-01-01
    • 2016-12-01
    • 1970-01-01
    • 2016-09-01
    • 2012-07-11
    • 2018-04-08
    • 2017-04-20
    • 2018-10-02
    • 2016-09-04
    相关资源
    最近更新 更多