如何在textwatcher中调用sql搜索查询?

时间:2016-10-01 13:20:23

标签: android

我在使用textwatcher过滤自定义列表视图中的项目时遇到问题。谁能帮助我如何在这个textwatcher中调用sql查询? Sql查询和我的自定义列表代码如下所示:

    public void searchValues(String mobile){
    try{
    open();

    String searchQuery = " select * from tbl_product where fld_mobile like  '%"+mobile+"%'";
    db.rawQuery(searchQuery, null);
    db.close();
    Log.v("Search_Query", searchQuery);
    }
    catch(Exception exp)
    {
        exp.printStackTrace();
}

}

Listview代码:

     public class GridViewActivity extends Activity  
        {  
       DBHelper dbhelper = null;
      ProductAdapter adapter;
       ListView gridView;
       @Override  
     protected void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);  
     setContentView(R.layout.gridview);  
    dbhelper=new DBHelper(this); 

    gridView=(ListView) findViewById(R.id.listView1);  
    //Add header of search bar
   LayoutInflater inflater = getLayoutInflater();
   ViewGroup header = (ViewGroup) inflater.inflate(R.layout.textview, gridView, false);
   gridView.addHeaderView(header, null, false);

   final ArrayList<Product> list=new ArrayList<Product>();  

    try{
        ArrayList<Product> productlist= dbhelper.getEmployee();  
        Log.v("Query Check", "Working");
        for(Product product : productlist){
        product.getMobile();
        Log.v("Get Name:", product.getMobile());
        product.getPrice();
        Log.v("Get No:", String.valueOf(product.getPrice()));
        list.add(product);
        adapter=new ProductAdapter(this, list);  
        gridView.setAdapter(adapter);

    }
    }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }

    EditText myFilter = (EditText) findViewById(R.id.editText1);
   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) {
  adapter.getFilter().filter(s.toString());
   }

   });

0 个答案:

没有答案