Click here to Skip to main content
15,898,222 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
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..

Java
	// 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;
            }
         }));
Posted
Updated 19-Oct-12 19:46pm
v3

1 solution

I have done it myself.Splitting a name by space and then checking the index of the typed string..
C#
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;
    }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900