【问题标题】:How to filter ListView using a EditText when enter is pressed?按下 Enter 时如何使用 EditText 过滤 ListView?
【发布时间】:2015-06-21 21:38:22
【问题描述】:

所以,现在,我有一个 EditText,它在文本更改时过滤 ListView 中的数据。但是,我想对其进行更改,使其仅在按下回车键时过滤。我好像搞不懂。

它从数据库中提取数据。

public class SearchActivity extends NavDrawerActivity {


    private DBHandler dbHelper;
    private SimpleCursorAdapter dataAdapter;
    private EditText myFilter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_main_activity3);

        FrameLayout frameLayout = (FrameLayout) findViewById(R.id.activity_frame);
        // inflate the custom activity layout
        LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View activityView = layoutInflater.inflate(R.layout.activity_main_activity3, null, false);
        // add the custom layout of this activity to frame layout.
        frameLayout.addView(activityView);

        dbHelper = new DBHandler(this);
        dbHelper.open();

        //Clean all data
        dbHelper.deleteAllRecipes();
        //Add some data
        dbHelper.insertSomeRecipes();

        //Generate ListView from SQLite Database
        displayListView();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    private void displayListView() {

        final Cursor cursor = dbHelper.fetchAllRecipes();

        // The desired columns to be bound
        String[] columns = new String[]{
                //DBHandler.COLUMN_CODE,
                DBHandler.COLUMN_NAME,
                DBHandler.COLUMN_TYPE,
                DBHandler.COLUMN_INGRED
        };

        // the XML defined views which the data will be bound to
        int[] to = new int[]{
                //R.id.code,
                R.id.name,
                R.id.type,
                R.id.ingredient,
        };

        // create the adapter using the cursor pointing to the desired data
        //as well as the layout information
        dataAdapter = new SimpleCursorAdapter(
                this, R.layout.recipeinfo,
                cursor,
                columns,
                to,
                0);

        ListView listView = (ListView) findViewById(R.id.listView1);
        // Assign adapter to ListView
        listView.setAdapter(dataAdapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> listView, View view,
                                    int position, long id) {
                // Get the cursor, positioned to the corresponding row in the result set
                //Cursor cursor = (Cursor) listView.getItemAtPosition(position);
                Intent n = new Intent(getApplicationContext(), RecipeActivity.class);
                n.putExtra("position", position);
                startActivity(n);
            }
        });

        final EditText myFilter = (EditText) findViewById(R.id.myFilter);
                myFilter.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) {
                        dataAdapter.getFilter().filter(s.toString());

                    }
                });

        dataAdapter.setFilterQueryProvider(new FilterQueryProvider() {
            public Cursor runQuery(CharSequence constraint) {
                return dbHelper.fetchRecipesByName(constraint.toString());
            }
        });

    }

}

【问题讨论】:

    标签: android listview filtering


    【解决方案1】:

    而不是这个:

    final EditText myFilter = (EditText) findViewById(R.id.myFilter);
    myFilter.addTextChangedListener(new TextWatcher() {
    ...
        public void onTextChanged(CharSequence s, int start,int before, int count) {
            dataAdapter.getFilter().filter(s.toString());
    
        }
    });
    

    试试下面:

    final EditText myFilter = (EditText) findViewById(R.id.myFilter);
    myFilter.setImeActionLabel("Filter",1);
    myFilter.setPrivateImeOptions("actionUnspecified");
    myFilter.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int id, KeyEvent keyEvent) {
                if (id == 1 || id == EditorInfo.IME_NULL) {
                    dataAdapter.getFilter().filter(textView.getText().toString());
                    return true;
                }
                return false;
            }
        });
    

    【讨论】:

    • 这对我来说真的不起作用。当我按 Enter 时,什么都没有显示。
    • 那么你的 dataAdapter.getFilter() 有问题...因为当你点击 enter 时,这个函数被调用
    • 好的,我检查一下。另外,'s.toString()' 中的 's' 应该是 'textView' 吗?
    • 我认为应该是 (textView.getText().toString) 而不是 s
    • 好的,它现在可以工作了 - 原来是问题所在。谢谢!
    【解决方案2】:

    在 xml 中设置为您的编辑文本 android:imeOptions="actionSearch"。

    myFilter.setOnEditorActionListener(new OnEditorActionListener() {
                @Override
                public boolean onEditorAction(TextView v, int actionId,
                        KeyEvent event) {
                      if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                        dataAdapter.getFilter().filter(v.getText.toString());
                    }
                    return false;
                }
            });
    

    【讨论】:

    • 这也不起作用。我收到关于“已处理”部分的错误。
    【解决方案3】:

    与其依赖 IME 选项,为什么不添加一个“完成”按钮并在按下时获取 EditText 的值?您仍然可以使用 TextWatcher 来确保输入有效并且没有输入无效字符。

    【讨论】:

      【解决方案4】:

      无需创建额外的过滤器类。您只需保留来自 TextWatcher 的字符串,然后使用包含方法检查该字符串与您的列表视图数据

      searchforcategory.addTextChangedListener(new TextWatcher() {
              @Override
              public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
      
              }
      
              @Override
              public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                  getAllCetegory(charSequence.toString());
      
              }
      
              @Override
              public void afterTextChanged(Editable editable) {
      
      
      
              }
          });
      

      现在只需使用您的数据检查该字符串

       for(int i=0;i<response.body().getDataList().size();i++){
      
                          if(response.body().getDataList().get(i).getName().contains(checkString)){
      
      
                              productDataLists.add(response.body().getDataList().get(i));
      
                          }
      
      
                      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-08
        • 1970-01-01
        • 1970-01-01
        • 2017-12-12
        • 2012-02-02
        • 2013-10-13
        相关资源
        最近更新 更多