2017-10-05 107 views
0

我只是想在我的Listview上实现一个搜索栏,它连接到我的SQLiteDatabase。正确使用FilterQueryProvider搜索Listview

我已经明白我需要实现setFilterQueryProvider,尽管在调试期间它看起来并且运行良好,但它不提供搜索结果。

我假设我缺少一个明显的步骤。

数据库查看

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

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

+0

我调试过程中已经注意到,我从来没有闯过第一如果回路fetchdatabyfilter()到[IF(行!= null)]和return语句。 –

+0

我想我已经解决了这个问题......抱歉哈哈。将稍后发布我的修复程序。虽然不介意听到正确的答案:) –

回答

0

以为我会发布我的修复程序为任何人有与我一样的问题。

数据库查看器中的OnCreate

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


} 

DatabaseHelper过滤功能

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