按Enter键时如何使用EditText过滤ListView?

时间:2015-04-15 18:54:07

标签: android listview filtering

所以,现在,我有一个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());
            }
        });

    }

}

4 个答案:

答案 0 :(得分:0)

而不是:

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

答案 1 :(得分:0)

设置为您的编辑文本android:imeOptions =&#34; actionSearch&#34;在xml。

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

答案 2 :(得分:0)

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

答案 3 :(得分:0)

无需创建额外的过滤器类。您只需保留TextWatcher的字符串,然后使用contains方法

将其与列表视图数据一起检查
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));

                    }


                }