在android中创建一个搜索框

时间:2013-04-15 06:31:56

标签: android search arraylist

我需要在android中创建一个search box,它会检查来自arraylist的数据,如果它在那里,它将返回到该页面。

代码:

 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)))
      {

      }
     }
    }

我获得了search functionality的上述代码但我需要用户search any keyword它将从array list检查,如果它在那里它会返回到该页面,否则它将会显示no data found

2 个答案:

答案 0 :(得分:0)

使用AutoCompleteTextView

编辑EditText

AutoCompleteTextView:我在ActionBar中使用过它。你也可以在活动中使用它。它非常简单。

有关详细信息,请查看此articalcode

对于Actionbar,您必须在代码中添加 android-suport-v4.jar 文件..

答案 1 :(得分:0)

您应该尝试使用Usage of SearchView in an ActionBar

的Android功能

还提供了Collapsable SearchView

的兼容性

以下是您可以通过ArrayList试用来实现搜索的方式:

  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" };
  }