【问题标题】:search with suggestion when edditext onchange event in android在 android 中的 edditext onchange 事件时搜索建议
【发布时间】:2015-08-12 11:30:43
【问题描述】:

我正在为 android 编写一个字典应用程序。我想在 edditext 键入时将搜索数据填充到列表视图。它可以搜索,但打字速度太慢了。请帮帮我。

txtKeyword.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) {
                String st = txtKeyword.getText().toString().trim();
                lvData = (ListView) findViewById(R.id.lvData);
                ArrayList<String> list = search(st);
                adapter = new VicnAdapter(getApplicationContext());
                adapter.setListView(list);
                lvData.setAdapter(adapter);
                restartSearch(st);
           }
        });

【问题讨论】:

    标签: android android-edittext onchange


    【解决方案1】:

    使用 Adapter 的 notifyDataSetChanged() 函数,而不是每次都创建 Adapter 的新对象,这可能会加快您的搜索速度

    【讨论】:

      【解决方案2】:

      问题是:

      1. 每次调用 onTextChanged() 时,您都会调用 findViewById()

      2. 每次调用 onTextChanged() 时,您都会创建一个新的 Adapter

      解决办法是:

      将这些代码移至您的onCreate()

      lvData = (ListView) findViewById(R.id.lvData);
      adapter = new VicnAdapter(getApplicationContext());
      lvData.setAdapter(adapter);
      

      并像这样修改您的onTextChanged()

      public void onTextChanged(CharSequence s, int start, int before, int count) {
                      String st = txtKeyword.getText().toString().trim();
                      list.addAll(search(st));
                      adapter.notifyDatasetChanged();                    
                      restartSearch(st);
                 }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-09-15
        • 2015-05-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多