0

我需要search box在 android 中创建一个检查数据的位置,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

4

2 回答 2

0

安装了 EditText 使用AutoCompleteTextView

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

有关更多详细信息,请查看这篇文章代码

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

于 2013-04-15T06:36:51.147 回答
0

您应该尝试使用在 ActionBar中使用 SearchView 的 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" };
  }
于 2013-04-15T06:52:35.467 回答