如何通过多个方式搜索listview中的数据?

时间:2015-04-29 02:42:04

标签: android listview

现在,我的应用程序会在将某些内容输入editText时过滤listview中的数据,但它一次只能过滤一件事。我希望它能够过滤超过价值。例如,如果有人输入“鸡肉”,它应该用“鸡肉”这个词过滤食谱。但是,如果有人输入“晚餐”,我希望它通过“鸡肉”和“晚餐”来过滤食谱。最后,我想让它成为列表视图上方的复选框,以便可以轻松删除它们。

我无法弄清楚如何做到这一点。我最初玩过循环,但实际上并没有到达任何地方。

public class SearchActivity extends NavDrawerActivity {


    private DBHandler dbHelper;
    private SimpleCursorAdapter dataAdapter;
    ArrayList<String> filters = new ArrayList<String>();
    //String[] filters;
    FrameLayout frameLayout;

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

        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, null, null, 1);
        //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);
                String recipeName = cursor.getString(cursor.getColumnIndexOrThrow("name"));
                Intent n = new Intent(getApplicationContext(), RecipeActivity.class);
                //n.putExtra("position", position);
                n.putExtra("recipeName", recipeName);
                startActivity(n);
            }
        });

        //final GridView gridView = (GridView)findViewById(R.id.gridView);
        final TextView tv = (TextView)findViewById(R.id.textView14);
        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) {
                    String filter = textView.getText().toString();
                    dataAdapter.getFilter().filter(filter);
                    filters.add(filter);
                    tv.append(filter);
                    myFilter.setText("");
                }
                return false;
            }
        });

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

    }

    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // Then you start a new Activity via Intent
        Intent intent = new Intent();
        intent.setClass(this, RecipeActivity.class);
        intent.putExtra("position", position);
        // Or / And
        intent.putExtra("id", id);
        startActivity(intent);

    }
}

DBHandler中的fetchRecipesByName

public Cursor fetchRecipesByName(String inputText) throws SQLException {
    SQLiteDatabase mDb = this.getWritableDatabase();
    Log.w(TAG, inputText);
    Cursor mCursor = null;
    if (inputText == null  ||  inputText.length () == 0)  {
        mCursor = mDb.query(SQLITE_TABLE, new String[] {COLUMN_ROWID,
                        COLUMN_NAME, COLUMN_TYPE, COLUMN_INGRED, COLUMN_IMGPATH},
                null, null, null, null, null);

    }
    else {

        mCursor = mDb.query(true, SQLITE_TABLE, new String[] {COLUMN_ROWID,
                        COLUMN_NAME, COLUMN_TYPE, COLUMN_INGRED, COLUMN_IMGPATH},
                COLUMN_NAME + " like '%" + inputText + "%'" + " or " +
                        COLUMN_TYPE + " like '%" + inputText + "%'" + " or " +
                        COLUMN_INGRED + " like '%" + inputText + "%'",
                null, null, null, null, null);
    }
    if (mCursor != null) {
        mCursor.moveToFirst();
    }
    return mCursor;

}

1 个答案:

答案 0 :(得分:0)

dbHelper.fetchReccipesByName()的实施是什么?我认为,截至目前,它仅通过一件事查询表格。您应该更改其逻辑并在此方法中实现您的复杂需求(显然,它应该是SQL查询执行)。

作为最佳做法,您应该调用listView.setFilterText()而不是dataAdapter.getFilter().filter(),因为此方法应该在辅助线程中运行,因为数据库查询非常耗时。如果您致电listView.setFilterText(),框架将处理线程并在辅助线程中调用filter.filter()

最后,由于您使用多个关键字进行搜索,但setFilterText()仅接受一个CharSequence参数,因此您应该将多个关键字编码为单个String(以逗号分隔) 。在查询时,您可以解码约束以获取关键字。