search a value from sqlite database and retrieve in listview
Asked Answered
K

3

2

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();
        }
Krp answered 29/7, 2013 at 13:8 Comment(2)
try this way: https://mcmap.net/q/274350/-how-to-perform-an-sqlite-query-within-an-android-applicationGraniah
What is Contactsnew ??Discuss
K
4

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!!

Krp answered 4/9, 2013 at 12:32 Comment(0)
M
3

put below method in your database class:

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;
    }

Now get this method in your activity and then Initialize that return arraylist into ListView...

Marengo answered 29/7, 2013 at 13:22 Comment(0)
G
0

try this way

or

Cursor cTitle=db.rawQuery("SELECT * FROM "+ Contactsnew.TABLE02 + " WHERE " 
                    + Contactsnew.userid + " = " + GetSet.getUserId() + " AND " + Contactsnew.TITLE +
                     " LIKE  '"+search.getText()+"%'");
Graniah answered 29/7, 2013 at 13:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.