0

I need to create a search box in android where it check the data from arraylist if it is in there it will return to that page.

code:

 String[] text = { "One", "Two", "Three", "Four", "Five", "Six", "Seven",
   "Eight", "Nine", "Ten" };

edittext.addTextChangedListener(new TextWatcher()
  {

   public void afterTextChanged(Editable s)
   {

   }

   public void beforeTextChanged(CharSequence s, int start,
    int count, int after)
   {

   }

   public void onTextChanged(CharSequence s, int start,
    int before, int count)
   {

    textlength = edittext.getText().length();
    text_sort.clear();
    image_sort.clear();

    for (int i = 0; i < text.length; i++)
    {
     if (textlength <= text[i].length())
     {
      if (edittext.getText().toString().
   equalsIgnoreCase((String) text[i].subSequence(0, textlength)))
      {

      }
     }
    }

i got the above code for search functionality But i need when user will search any keyword it will check from the array list ,if it is in there it will return to that page else it will show no data found!

2 Answers2

0

insted of EditText use AutoCompleteTextView

AutoCompleteTextView : here i have used that in ActionBar. you can also use that in activity. Its very simple.

for more detail check this artical and code.

for Actionbar you have to add android-suport-v4.jar file in your code..

Community
  • 1
  • 1
Dhaval Parmar
  • 18,812
  • 8
  • 82
  • 177
  • @thanks Dhaval Sodha Parmar whether it will support every version of android device!! –  Apr 15 '13 at 06:38
  • Dhaval Sodha Parmar thanks can i create custom listview here!! –  Apr 15 '13 at 11:41
  • @ Dhaval Sodha Parmar custom listview using adapter –  Apr 15 '13 at 11:54
  • @priya2134412: yes... http://wptrafficanalyzer.in/blog/customizing-autocompletetextview-to-display-images-and-text-in-the-suggestion-list-using-simpleadapter-in-android/ – Dhaval Parmar Apr 15 '13 at 12:04
0

You should try with the android functionality which is Usage of SearchView in an ActionBar

There is also its compatibility provided Collapsable SearchView

Below is the way you can implement the searching from the ArrayList try out:

  public class SearchActivity extends ListActivity
   {
     private ArrayAdapter<String> m_arrayAdapter;
     private SearchView m_searchView;
@Override
protected void onCreate(Bundle p_savedInstanceState)
{
    super.onCreate(p_savedInstanceState);
    // setContentView(R.layout.search);
    m_arrayAdapter =
            new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, m_items);
    setListAdapter(m_arrayAdapter);
}
public boolean onCreateOptionsMenu(android.view.Menu menu)
{
    // MenuInflater inflater = getMenuInflater();
    // inflater.inflate(R.menu.activity_main, menu);
    getMenuInflater().inflate(R.menu.activity_main, menu);
    m_searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
    m_searchView.setSubmitButtonEnabled(true);//Set the submit button on search view.
    // Associate searchable configuration with the SearchView
    // SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    // SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
    // searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    m_searchView.setOnQueryTextListener(new OnQueryTextListener()
    {
        @Override
        public boolean onQueryTextSubmit(String p_query)
        {
            m_arrayAdapter.getFilter().filter(p_query);
            return true;
        }
        @Override
        public boolean onQueryTextChange(String p_newText)
        {
            m_arrayAdapter.getFilter().filter(p_newText);
            return false;
        }
    });
    return true;
};
String[] m_items = new String[] { "China", "India", "United States", "Indonesia", "Brazil",
        "Pakistan", "Nigeria", "Bangladesh", "Russia", "Japan", "Mexico", "Philippines",
        "Vietnam", "Ethiopia", "Egypt", "Germany", "Iran", "Turkey",
        "Democratic Republic of the Congo", "Thailand", "France", "United Kingdom", "Italy",
        "South Africa", "Myanmar", "South Korea", "Colombia", "Spain", "Ukraine", "Tanzania",
        "Kenya", "Argentina", "Poland", "Algeria", "Canada" };
  }
GrIsHu
  • 29,068
  • 10
  • 64
  • 102
  • thanks but what i want is i don't want to display any listview while search,once the user type the keyword it will search from the arraylist whether the keyword is present or not ! if is there it will return to that page else it will display as no data found! am i clear!! –  Apr 15 '13 at 06:55
  • If you do not want the listview then simply you can remove the code of the Listview directly get the results directly from the ArrayList. – GrIsHu Apr 15 '13 at 07:11