需要Android ||的帮助操作者

时间:2016-04-20 09:14:06

标签: java android

我想说,'搜索用户的名字或用户的号码,但这不起作用:

if (wp.getName().toLowerCase(Locale.getDefault())   
    .contains(charText)) || (wp.getPhone().toLowerCase(Locale.getDefault())  
    .contains(charText))  

关于如何让这个工作的任何想法?这是我的完整代码:

 public void filter(String charText) {
            charText = charText.toLowerCase(Locale.getDefault());
    //        _data is our list of contacts
            _data.clear();
    //        If there is nothing in the searchview, if the charText length is 0,
    //        then show all the contacts
            if (charText.length() == 0) {
                _data.addAll(arraylist);
    //            or else....
            } else {
                for (SelectContact wp : arraylist) {
    //                If a contact's name matches the input thus far, which is charText, or their number matches it,
    //                then include it in the listview.
                    if (wp.getName().toLowerCase(Locale.getDefault())
                            .contains(charText)) || (wp.getPhone().toLowerCase(Locale.getDefault())
                            .contains(charText))
                         {

                            _data.add(wp);
                        }
                }
            }
            notifyDataSetChanged();
        }

1 个答案:

答案 0 :(得分:1)

正如鲍勃·马洛加在上面的评论中指出的那样,总是将你的if条件的整个括在括号中,比如

if (put the whole condition of what your looking out for in here)
{do stuff}