2

如何在sqlite databasea 中找到确切的数据并检索它listactivity?我试过这样,但我没有得到价值。

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();
        }
4

3 回答 3

4

在您的 sqliteconnecter 中创建一个方法并从您各自的类中调用该方法。现在在你的 sqlite 类中输入以下代码。

Cursor cusror;

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

在字符串中打印查询并检查您是否获得了正确的值,然后返回光标。

试试这个然后说!!

于 2013-09-04T12:32:29.777 回答
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;
    }

现在在您的活动中获取此方法,然后将返回的数组列表初始化为 ListView...

于 2013-07-29T13:22:01.843 回答
0

试试这个方法

或者

Cursor cTitle=db.rawQuery("SELECT * FROM "+ Contactsnew.TABLE02 + " WHERE " 
                    + Contactsnew.userid + " = " + GetSet.getUserId() + " AND " + Contactsnew.TITLE +
                     " LIKE  '"+search.getText()+"%'");
于 2013-07-29T13:27:00.563 回答