使用FilterQueryProvider正确搜索Listview

时间:2017-10-05 18:45:04

标签: android database sqlite android-sqlite

我的目标只是在我的Listview上实现一个搜索栏,它连接到我的SQLiteDatabase。

我被引导理解我需要实现setFilterQueryProvider,虽然在调试期间它看起来并且运行正常但它没有提供搜索结果。

我假设我在这里错过了一个明显的步骤。

数据库查看器的OnCreate

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_database_viewer);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    customerlist = (ListView) findViewById(R.id.ListViewCustomers);
    SearchBar = (EditText)findViewById(R.id.SearchBarET);



    Cursor c = mydb.getallrows();
    customerlist = (ListView) findViewById(R.id.ListViewCustomers);
    String[] fromfieldnames = new String[] {DatabaseHelper.COL_1, DatabaseHelper.COL_2, DatabaseHelper.COL_3, DatabaseHelper.COL_4};
    int[] tofieldnames = new int[] {R.id.TVCUSTOMERNAME, R.id.TVADDRESS, R.id.TVMARKS, R.id.TVID};
    final SimpleCursorAdapter adapter;
    adapter = new SimpleCursorAdapter(getBaseContext(), R.layout.custom_db_viewer_row, c, fromfieldnames, tofieldnames, 0);
    customerlist.setAdapter(adapter);
    SearchBar.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) {
            adapter.getFilter().filter(charSequence.toString());

        }

        @Override
        public void afterTextChanged(Editable editable) {


        }
    });

    adapter.setFilterQueryProvider(new FilterQueryProvider() {
        @Override
        public Cursor runQuery(CharSequence charSequence) {
            return mydb.fetchdatabyfilter(charSequence.toString());
        }
    });


    customerlist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int i, long l) {
            DisplayID = (TextView)  findViewById(R.id.TVCUSTOMERNAME);
            DisplayMarks = (TextView)  findViewById(R.id.TVADDRESS);
            DisplayName = (TextView)  findViewById(R.id.TVID);
            DisplayAddress = (TextView)  findViewById(R.id.TVMARKS);

            String a = Long.toString(l);


            Cursor c;

            // NEED TO MAKE A CURSOR THAT GETS ALL ROWS NOT COLUMNS LIKE BELOW.
            // 35 ROWS 4 COLUMNS.

            Intent clientViewIntent = new Intent(DatabaseViewer.this, ClientViewer.class);

            clientViewIntent.putExtra("Client ID", a);
            clientViewIntent.putExtra("Client Name", a);
            clientViewIntent.putExtra("Client Address", a);

            startActivity(clientViewIntent);
        }
    });


}

DatabaseHelper过滤器功能

    public Cursor fetchdatabyfilter(String inputText) {
    Cursor row = null;
    String query = "SELECT * FROM "+TABLE_NAME;
    if (inputText == null  ||  inputText.length () == 0)  {
        row = sqldb.rawQuery(query, null);
    }else {
        query = "SELECT * FROM "+TABLE_NAME+" WHERE "+COL_2+" like '%"+inputText+"%'";
        row = sqldb.rawQuery(query, null);
    }
    if (row != null) {
        row.moveToFirst();
    }
    return row;
}

我认为这就是你应该需要的一切。

1 个答案:

答案 0 :(得分:0)

以为我会为遇到与我有同样问题的人发布我的修复。

数据库查看器的OnCreate

this.Content.Width

DatabaseHelper过滤器功能

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_database_viewer);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    customerlist = (ListView) findViewById(R.id.ListViewCustomers);
    SearchBar = (EditText)findViewById(R.id.SearchBarET);



    Cursor c = mydb.getallrows();
    customerlist = (ListView) findViewById(R.id.ListViewCustomers);
    String[] fromfieldnames = new String[] {DatabaseHelper.COL_1, DatabaseHelper.COL_2, DatabaseHelper.COL_3, DatabaseHelper.COL_4};
    int[] tofieldnames = new int[] {R.id.TVCUSTOMERNAME, R.id.TVADDRESS, R.id.TVMARKS, R.id.TVID};
    final SimpleCursorAdapter adapter;
    adapter = new SimpleCursorAdapter(getBaseContext(), R.layout.custom_db_viewer_row, c, fromfieldnames, tofieldnames, 0);
    customerlist.setAdapter(adapter);
    SearchBar.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) {
            adapter.getFilter().filter(charSequence.toString());

        }

        @Override
        public void afterTextChanged(Editable editable) {


        }
    });

    adapter.setFilterQueryProvider(new FilterQueryProvider() {
        @Override
        public Cursor runQuery(CharSequence charSequence) {
            return mydb.fetchdatabyfilter(charSequence.toString());
        }
    });


    customerlist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int i, long l) {
            DisplayID = (TextView)  findViewById(R.id.TVCUSTOMERNAME);
            DisplayMarks = (TextView)  findViewById(R.id.TVADDRESS);
            DisplayName = (TextView)  findViewById(R.id.TVID);
            DisplayAddress = (TextView)  findViewById(R.id.TVMARKS);

            String a = Long.toString(l);


            Cursor c;

            // NEED TO MAKE A CURSOR THAT GETS ALL ROWS NOT COLUMNS LIKE BELOW.
            // 35 ROWS 4 COLUMNS.

            Intent clientViewIntent = new Intent(DatabaseViewer.this, ClientViewer.class);

            clientViewIntent.putExtra("Client ID", a);
            clientViewIntent.putExtra("Client Name", a);
            clientViewIntent.putExtra("Client Address", a);

            startActivity(clientViewIntent);
        }
    });


}