Android的过滤器列表视图自定义适配器 [英] Android filter listview custom adapter
问题描述
大家好我在做一个应用程序,它有一个EditText搜索的列表视图项目。如果用户键入一个字母。这些数据来自于我的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中的字母,所有开始与该字母的项目应进行过滤。但是,使用code我张贴以上,它不会做正是我想要的。它只是过滤器,每当我打了整个项目的名称。有任何想法吗?您的帮助将大大AP preciated。谢谢你。
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.
推荐答案
据我可以看到你只检查,如果两个字符串完全匹配。 你可能想使用<一个href="http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#startsWith%28java.lang.String%29"相对=nofollow>布尔startsWith(字符串preFIX)
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屋!