3

I am making app for finding number plates of india. my database contains two columnns "code" and "city" code contains data like MH1,MH2 etc. and city contains data like Pune,Mumbai.

App contains one edittext box and listview.

Listview consists whole data from database like GJ3 Rajkot, GJ10 Jamnagar etc.

if i write GJ in edittext box whole only data of GJ must be apperaed in listview.

Pavan Anadkat
  • 111
  • 1
  • 3
  • 9
  • 2
    use text watcher for EditText and when text changes then execute your query in database and in query use "like" incited of "=" – Jaiprakash Soni Oct 11 '12 at 08:55
  • Thanks but i am not getting your ans, can u write a query ? – Pavan Anadkat Oct 11 '12 at 08:58
  • [see this tuts](http://samir-mangroliya.blogspot.in/2012/05/android-sectioned-listview-with-search_6865.html) – Samir Mangroliya Oct 11 '12 at 10:05
  • not working..coz i have already use one cursor for fetching data from database...and added to the list view..so how can i use another cursor for searching data using edittext and display data in listview accordoing to code typed in editttext – Pavan Anadkat Oct 13 '12 at 08:25

2 Answers2

2

Here is the query : SELECT * FROM table_name WHERE code LIKE '%GJ%' LIMIT 0 , 30

Jaiprakash Soni
  • 4,100
  • 5
  • 36
  • 67
2

Try this :

Cursor c = mDb.rawQuery(
            "select * from table_name where column_name = ?",
            new String[] { "search" });

EDIT :

Follow the tutorial of the given above link and add below method into TestAdapter

public Cursor get_tag_Data()
 {
     try
     {
         String sql ="select * from table_name where column_name = ?", new String[] { edittext.getText().toString().trim()}";

         Cursor mCur = mDb.rawQuery(sql, null);
         if (mCur!=null)
         {
            mCur.moveToNext();
         }
         return mCur;
     }
     catch (SQLException mSQLException) 
     {
         Log.e(TAG, "getTestData >>"+ mSQLException.toString());
         throw mSQLException;
     }
 }

and call this method from your class like:

 TestAdapter mDbHelper = new TestAdapter(urContext);        
 mDbHelper.createDatabase();      
mDbHelper.open();

Cursor testdata = mDbHelper.get_tag_Data();

mDbHelper.close();

EDIT:

Declare below method into your database class,

 public List<String> getQuestions(String difficulty) {

   public static List<String> question_Set;
    question_Set = new ArrayList<String>();


    Cursor c = mDb.rawQuery(
            "select * from table_name where column_name = ?", new String[] { difficulty });

    while (c.moveToNext()) {

        question_Set.add(c.getString(1).trim());


    }

    return question_Set;
}

Now call like

DB.getQuestions(edittext.getText().toString().trim()); // DB is your database class name
Rahul Patel
  • 3,823
  • 5
  • 30
  • 46
  • I think you want to search as per edittext value into column.if yes then put your edittext value in place of search. like `"select * from table_name where column_name = ?", new String[] { edittext.getText().toString().trim()}` – Rahul Patel Oct 11 '12 at 10:31
  • still i am not getting. If i write AP in edittext then in listview it must display a list of AP's city and code. – Pavan Anadkat Oct 11 '12 at 13:27
  • How are you accessing the database code?, I don't know. But check this link for the database http://stackoverflow.com/a/9109728/1263679 I think it's help you. – Rahul Patel Oct 12 '12 at 03:45
  • not working..coz i have already use one cursor for fetching data from database...and added to the list view..so how can i use another cursor for searching data using edittext and display data in listview accordoing to code typed in editttext – Pavan Anadkat Oct 13 '12 at 08:25
  • @PavanAnadkat please check my updated code and follow the http://stackoverflow.com/a/9109728/1263679 – Rahul Patel Oct 13 '12 at 08:40
  • i have to add a new class Test Adapter ?? – Pavan Anadkat Oct 13 '12 at 08:40
  • I am edit code for the given link, in that already create the testAdapter class. but no problem if you are going different way then add this method where you already create first cursor query and call same as previous one. – Rahul Patel Oct 13 '12 at 08:44
  • Please can you show me how you are coding for the first cursor? – Rahul Patel Oct 13 '12 at 08:47
  • i am enable to search a data from database for example if i write AR in edittext it display all records of AR but it display only in Log cat...Whenever i type anything in Edittext box Listview is diappeared..now i want to display the same data which displays in logcat – Pavan Anadkat Oct 13 '12 at 09:40
  • if (cursor.moveToFirst()) { do { String code = cursor.getString(0); String city = cursor.getString(1); Log.d("testdata", code + "" + city); arrayList.add(code + "\t" + ":" + "" + city); //arrayList.add(data); arrayList.addAll(arrayList); adapter.clear(); adapter.notifyDataSetChanged(); } while (cursor.moveToNext()); – Pavan Anadkat Oct 13 '12 at 12:49
  • if you have no problem then accept answer as true or if possible then give upvote. – Rahul Patel Oct 17 '12 at 06:12