根据名称在Android中过滤Listview [英] Filtering Listview according to name in android
问题描述
我正在android上做一个小工作.它有一个列表视图,其中包含一些朋友的名字和他们的生日.有一个搜索文本框来过滤列表视图.我还添加了过滤功能.一个名称可能包含3个部分(第一,中间,最后),所以我想根据名称的每个部分的首字母过滤列表视图.我的代码现在仅在全名包含键入字母时过滤.考虑一个示例,我有2个朋友,名称是Shezan Mahmud,Bob Ericson.如果我在搜索文本框中输入字母"e",我的代码将显示包含这两个名称的listview输出(因为两个名称都包含字母"e").但是我想要包含名称Bob Ericson的输出(因为该名称的第二部分包含字母"e"作为第一个字母).任何建议如何完成?我的代码在这里..
I am doing a small work on android.It has a listview which contains some friend''s name and their birthday.There is a search textbox to filter the listview.I also add filtering functionality.As a name may contain 3 parts(first,middle,last),so i want to filter the listview according to first letter of every part of name.My code now filters only when the whole name contains the typed letter.Consider an example,I have 2 friends which name is Shezan Mahmud,Bob Ericson.if i type a letter ''e'' in the search textbox my code shows the listview output containing those 2 names(as both 2 names contain the letter ''e''). But i want the output which contains the name,Bob Ericson(because 2nd part of this name contains the letter ''e'' as first letter).Any suggestion how can it be done?My code''s here..
// The view in this tabbed example
private ListView ls1;
private TabHost tabHost;
public static String[] friends={"Shuvro Pal","Tamanna Hossain Trena","Ariful Islam","Nargis Rahman"};
public static String[] birthdate={"1989","1989","1990","1985"};
private EditText filterText;
private ArrayList<String> arr_sort= new ArrayList<String>();
int textLength=0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
filterText = (EditText) findViewById(R.id.search_box);
tabHost = getTabHost();
tabHost.setOnTabChangedListener(this);
// setup list view 1
ls1 = (ListView) findViewById(R.id.list1);
final List<Model> list = new ArrayList<Model>();
final ArrayAdapter<Model> adapter = new MyCustomArrayAdapter(this, list);
ls1.setAdapter(adapter);
for(int i=0;i<friends.length;i++)
list.add(get(friends[i],birthdate[i]));
filterText.addTextChangedListener(new TextWatcher()
{
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
textLength=filterText.getText().length();
list.clear();
for(int i=0;i<friends.length;i++)
{
if(textLength<=friends[i].length())
{
String[] word=friends[i].split(" ");
for(int j=0;j<word.length;j++)
{
if(friends[i].toLowerCase().contains(filterText.getText().toString().toLowerCase()))
{
list.add(get(friends[i],birthdate[i]));
break;
}
}
}
}
ls1.setAdapter(adapter);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,int after)
{
public void afterTextChanged(Editable s)
{
}
});
// add views to tab host
tabHost.addTab(tabHost.newTabSpec(LIST1_TAB_TAG).setIndicator(LIST1_TAB_TAG).setContent(new TabContentFactory()
{
public View createTabContent(String arg0)
{
return ls1;
}
}));
推荐答案
我自己完成的.用空格分隔名称,然后检查键入的字符串的索引. >
I have done it myself.Splitting a name by space and then checking the index of the typed string..
String[] word1=friends[i].split(" ");
for(int a=0;a<word1.length;a++)
{
index=word1[a].toLowerCase().indexOf(filterText.getText().toString().toLowerCase());
if(index == 0)
{
list.add(get(friends[i],birthdate[i]));
break;
}
这篇关于根据名称在Android中过滤Listview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!