2013-07-29 158 views
2

如何找到sqlite database中的確切數據並在listactivity中檢索它?我嘗試這樣,但我沒有得到價值。從sqlite數據庫中搜索一個值,並在列表視圖中檢索

search = (EditText) findViewById(R.id.alertsearch); 

search.addTextChangedListener(new TextWatcher(){ 

     @Override 
     public void afterTextChanged(Editable s) { 
      // TODO Auto-generated method stub 
      list2.clear(); 
      db = openOrCreateDatabase(Contactsnew.DB_NAME, MODE_PRIVATE, null); 
      Cursor cTitle=db.rawQuery(" SELECT * FROM "+ Contactsnew.TABLE02 + " WHERE " 
        + Contactsnew.userid + " = " + GetSet.getUserId() + " AND " + Contactsnew.TITLE + 
        " LIKE ? " ,new String []{search.getText()+"%"}); 
      custAdapter.changeCursor(cTitle); 

      while(cTitle.moveToNext()){ 
       list2.add(cTitle.getString(2)); 
       } 
      cTitle.close(); 
     } 
+0

試試這種方式:http://stackoverflow.com/a/1243378/1168654 –

+0

什麼是Contactsnew? – 2014-06-30 09:08:25

回答

4

在你的sqliteconnecter中創建一個方法並在你的相應類中調用該方法。現在typr下面的代碼在烏拉圭回合sqlite的類,

Cursor cusror; 

cursor=db.rawQuery("SELECT * FROM "+ Contactsnew.TABLE02 + " WHERE " 
       + Contactsnew.userid + " = " + Contactsnew.userId + " AND " + Contactsnew.TITLE + 
       " LIKE '"+search.getText()+"%'"); 

打印字符串中的查詢,並檢查您是否得到正確的值,然後返回光標。

試試這個,說!

3

下面放方法在你的數據庫類:

public ArrayList<String> getlist(String search) { 

     ArrayList<String> alTasklist = new ArrayList<String>(); 

     try { 
      Cursor mCursor = db.query(true, Contactsnew.TABLE02, 
        new String[] { your field list }, SearchColumnname + "='" + search 
          + "'", null, null, null, null, null); 

      if (mCursor != null) { 
       mCursor.moveToFirst(); 
       for (int i = 0; i < mCursor.getCount(); i++) { 
        alTasklist.add(mCursor.getString(0)); 
        mCursor.moveToNext(); 
       } 
       mCursor.close(); 
       return alTasklist; 
      } 
      return alTasklist; 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

     return alTasklist; 
    } 

現在得到這個方法在你的活動,然後初始化數組列表返回到列表視圖...

0

嘗試this way

Cursor cTitle=db.rawQuery("SELECT * FROM "+ Contactsnew.TABLE02 + " WHERE " 
        + Contactsnew.userid + " = " + GetSet.getUserId() + " AND " + Contactsnew.TITLE + 
        " LIKE '"+search.getText()+"%'");