在自定义ListView中搜索

时间:2015-01-18 14:43:55

标签: android arrays listview search searchview

搜索中的问题只是从第一个单词开始 例如:搜索“皇家马德里”,如果搜索“马德里”没有给出结果,但如果搜索“真实”给我结果因为“真实”这个词是第一个单词 那么这个问题的解决方案是什么?谢谢:))

public void setAdapterToListview() {
    objAdapter = new LatestGridAdapter(getActivity(), R.layout.latest_lsv_item,
            arrayOfLatestVideo,columnWidth);
    lsv_latest.setAdapter(objAdapter);
}

public void showToast(String msg) {
    Toast.makeText(getActivity(), msg, Toast.LENGTH_LONG).show();
}

public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.menu_search, menu);

    final SearchView searchView = (SearchView) menu.findItem(R.id.search)
            .getActionView();

    final MenuItem searchMenuItem = menu.findItem(R.id.search);
    searchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {

        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            // TODO Auto-generated method stub
            if(!hasFocus) {
                 searchMenuItem.collapseActionView();
                searchView.setQuery("", false);
            }
        }
    });


    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextChange(String newText) {

            textlength=newText.length();
            arrayOfLatestVideo.clear();

            for(int i=0;i< allArrayVideoName.length;i++)
            {
                if(textlength <= allArrayVideoName[i].length())
                {
                    if(newText.toString().equalsIgnoreCase((String) allArrayVideoName[i].substring(0, textlength)))
                    {


                        ItemLatest objItem = new ItemLatest();

                        objItem.setId(Integer.parseInt(allArrayVideoId[i]));
                        objItem.setCategoryId(Integer.parseInt(allArrayVideoCatId[i]));
                        objItem.setCategoryName(allArrayVideoCatName[i]);
                        objItem.setDescription(allArrayVideoDesc[i]);
                        objItem.setVideoId(allArrayVideo[i]);
                        objItem.setVideoName(allArrayVideoName[i]);
                        objItem.setVideoUrl(allArrayVideourl[i]);
                        objItem.setImageUrl(allArrayImageUrl[i]);
                        arrayOfLatestVideo.add(objItem);

                    }
                }
            }

            setAdapterToListview();
            return false;
        }

        @Override
        public boolean onQueryTextSubmit(String query) {
            // Do something
            return true;
        }
    });

}

1 个答案:

答案 0 :(得分:1)

尝试使用this方法进行搜索。如果它返回-1以外的任何值,则意味着它找到了匹配项。我将所有内容都变为小写,这样您就不必担心区分大小写了。

    @Override
    public boolean onQueryTextChange(String newText) {

        textlength=newText.length();
        arrayOfLatestVideo.clear();

        for(int i=0;i< allArrayVideoName.length;i++)
        {
            if(textlength <= allArrayVideoName[i].length())
            {
                if(allArrayVideoName[i].toLowerCase().indexOf(newText.toLowerCase())!=-1)
                {
                    ItemLatest objItem = new ItemLatest();

                    objItem.setId(Integer.parseInt(allArrayVideoId[i]));
                    objItem.setCategoryId(Integer.parseInt(allArrayVideoCatId[i]));
                    objItem.setCategoryName(allArrayVideoCatName[i]);
                    objItem.setDescription(allArrayVideoDesc[i]);
                    objItem.setVideoId(allArrayVideo[i]);
                    objItem.setVideoName(allArrayVideoName[i]);
                    objItem.setVideoUrl(allArrayVideourl[i]);
                    objItem.setImageUrl(allArrayImageUrl[i]);
                    arrayOfLatestVideo.add(objItem);

                }
            }
        }

        setAdapterToListview();
        return false;
    }