Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
306 views
in Technique[技术] by (71.8m points)

android - search a value from sqlite database and retrieve in listview

How to find the exact data in sqlite database and retrieve it in a listactivity? I tried like this but I didn't get the value.

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();
        }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In your sqliteconnecter create a method and call that method from your respective class. Now type the following code in ur sqlite class.

Cursor cusror;

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

print the query in a string and check whether you are getting the correct values, then return the cursor.

try this and say!!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...