2017-10-05 108 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; 
}