Android过滤器列表视图自定义适配器 [英] Android filter listview custom adapter

查看:38
本文介绍了Android过滤器列表视图自定义适配器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我正在做一个应用程序,它有一个编辑文本来搜索列表视图上的项目.如果用户键入一个字母.数据来自我的 json 字符串(数据库),然后显示在我的列表视图中.到目前为止,这是我尝试过的:

Hello everybody I'm doing an app which has an edittext to search for items on the listview. If the user types a letter. The data come from my json string (database) and then display on my listview. So far this is what I've tried:

 ListViewAdapter adapter2;
 ArrayList<HashMap<String, String>> arraylist;
 ArrayList<String> list = new ArrayList<String>();

 wsSearch.addTextChangedListener(new TextWatcher (){

        public void afterTextChanged(Editable cs) {
            // TODO Auto-generated method stub



        }

        public void beforeTextChanged(CharSequence arg0, int arg1,
                int arg2, int arg3) {
            // TODO Auto-generated method stub

        }

        public void onTextChanged(CharSequence cs, int arg1, int arg2,
                int arg3) {
            // TODO Auto-generated method stub
            //BAPTISMAL_SONG.this.adapter2.getFilter().filter(cs); 


            String searchString = cs.toString();
            if(searchString.length() != 2) {
                adapter2 = new ListViewAdapterBaptismal(BAPTISMAL_SONG.this, arraylist);
                listview.setAdapter(adapter2);
                return;
            }
            ArrayList<HashMap<String, String>> arrayTemplist = new ArrayList<HashMap<String,String>>();
            for (int i = 0; i < arraylist.size(); i++)
                {
                String currentString = arraylist.get(i).get(BAPTISMAL_SONG.TAG_TITLE);
                if (searchString.equalsIgnoreCase(currentString))
                    {
                        arrayTemplist.add(arraylist.get(i));
                    }
                }
            adapter2 = new ListViewAdapterBaptismal(BAPTISMAL_SONG.this, arrayTemplist);
            listview.setAdapter(adapter2);

        }

    });

@Override
protected void onPostExecute(Void args) {
        // Locate the listview in listview_main.xml

        // Pass the results into ListViewAdapter.java
        adapter2 = new ListViewAdapter(Activity2.this, arraylist);
        // Binds the Adapter to the ListView
        listview.setAdapter(adapter2);


        // Close the progressdialog
        mProgressDialog.dismiss();
    }
}

我想要实现的是,如果用户输入一个像 B 这样的字母,那么所有以该字母开头的项目都应该被过滤掉.但是使用我上面发布的代码,它并不完全符合我的要求.它只是在我输入整个项目名称时进行过滤.有任何想法吗?对你的帮助表示感谢.谢谢.

What I want to achieve, if the user types a letter like B, all the items that start with the said letter should be filtered. But using the code I posted above, it does not do exactly what I want. It just filters whenever I typed the whole item name. Any ideas? Your help will be greatly appreciated. Thanks.

推荐答案

据我所知,您只是在检查两个字符串是否完全匹配.您可能想要使用 布尔值startsWith(字符串前缀)

As far as I can see you are only checking if the two Strings match exactly. You might want to use boolean startsWith(String prefix)

哪个会像

if (currentString.trim().toLowerCase().startsWith(searchString.trim().toLowerCase()))
{
     arrayTemplist.add(arraylist.get(i));
} 

这篇关于Android过滤器列表视图自定义适配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆