【问题标题】:SearchView Widget is not working properlySearchView 小部件无法正常工作
【发布时间】:2014-06-15 18:56:48
【问题描述】:

我正在尝试使用 searchview acitonbar 小部件过滤我的列表视图,但是当我在 edittext 中键入内容时,屏幕上会出现一个黑框。如何在没有那个黑框的情况下进行搜索。

这是我的代码。

public class MainActivity extends ListActivity implements OnQueryTextListener{

    private Button favListBtn;
    private ListView mListView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String[] categories =  {"Food", "Cloth", "Shoes","Agriculture",
                                "Food", "Cloth", "Shoes","Agriculture",
                                "Food", "Cloth", "Shoes","Agriculture",
                                "Food", "Cloth", "Shoes","Agriculture",
                                "Food", "Cloth", "Shoes","Agriculture",
                                "Food", "Cloth", "Shoes","Agriculture",
                                "Food", "Cloth", "Shoes","Agriculture"};

        mListView = (ListView) findViewById(android.R.id.list);
        mListView = getListView();

        ArrayAdapter<String> mAdapte = new ArrayAdapter<String>(MainActivity.this, 
                android.R.layout.simple_list_item_1, categories);
        mListView.setAdapter(mAdapte);
        mListView.setTextFilterEnabled(true);

        mListView.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                      int position, long id) {

                      new SearchAsynTask(MainActivity.this).execute();
                      // selected item 
                      String category = ((TextView) view).getText().toString();
                      Intent intent = new Intent(MainActivity.this, BusinessListActivity.class);
                      startActivity(intent);
                      Toast.makeText(MainActivity.this, category, Toast.LENGTH_LONG).show();
                      }
            });       
        }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        SearchManager searchManager = (SearchManager) getSystemService( Context.SEARCH_SERVICE );
        SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();

        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        searchView.setSubmitButtonEnabled(true);
        searchView.setOnQueryTextListener(this);

        return super.onCreateOptionsMenu(menu);

    }

    @Override
    public boolean onQueryTextSubmit(String query) {        

        return false;
    }

    @Override
    public boolean onQueryTextChange(String query) {
        if (TextUtils.isEmpty(query))
          {
                mListView.clearTextFilter();
            }
          else
          {
                mListView.setFilterText(query.toString());
            }
        return true;
    }
    }

【问题讨论】:

  • @Tim 我已经更新了我的问题。
  • 我想是因为你的 Toast.makeText(MainActivity.this, category, Toast.LENGTH_LONG).show();

标签: android listview android-widget searchview


【解决方案1】:

它显示了那个黑框,因为你设置了列表视图这样做:mListView.setTextFilterEnabled(true);

编辑: 您说没有上述行就无法进行搜索。这是因为您将文本过滤用于搜索目的。这不是一个好方法。您在onQueryTextChange() 回调中调用mListView.setFilterText(query.toString());。因此,如果不启用文本过滤,您的搜索机制将不起作用。

正确的方法是让您的 ListView Adapter 实现Filterable 接口并在那里处理过滤。

那么你应该搜索Filter对象:Filter.performFiltering(CharSequence)

【讨论】:

  • 没有这一行,我无法在我的列表视图中搜索。
猜你喜欢
  • 1970-01-01
  • 2020-12-18
  • 2014-10-20
  • 1970-01-01
  • 2022-11-11
  • 2022-11-04
  • 2021-04-04
  • 1970-01-01
  • 2020-09-13
相关资源
最近更新 更多