android - Can't get search function to list out serach result -
this database store information display on the
list.
public class databasehandler extends sqliteopenhelper { // static variables // database version private static final int database_version = 1; // database name private static final string database_name = "imagedb"; private static final string tag = "databasehandler"; // contacts table name private static final string table_contacts = "contacts"; // contacts table columns names public static final string key_id = "id"; public static final string key_name = "name"; public static final string key_price = "price"; public static final string key_image = "image"; public static final string key_search = "searchdata"; public databasehandler(context context) { super(context, database_name, null, database_version); } // creating tables @override public void oncreate(sqlitedatabase db) { string create_contacts_table = "create table " + table_contacts + "(" + key_id + " integer primary key," + key_name + " text,"+ key_price + " text," + key_search + " text," +" unique (" + key_name + "));" + key_image + " blob" + ")"; log.w(tag, create_contacts_table); db.execsql(create_contacts_table); } // upgrading database @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { log.w(tag, "upgrading database version " + oldversion + " " + newversion + ", destroy old data"); // drop older table if existed db.execsql("drop table if exists " + table_contacts); // create tables again oncreate(db); } /** * crud(create, read, update, delete) operations */ public// adding new contact long addcontact(contact contact) { sqlitedatabase db = this.getwritabledatabase(); contentvalues values = new contentvalues(); string searchvalue = contact._name + " "; values.put(key_name, contact._name); // contact name values.put(key_price, contact._price); // contact name values.put(key_image, contact._image); // contact phone values.put(key_search, searchvalue); // contact phone // inserting row return db.insert(table_contacts, null, values); } // getting contacts public list<contact> getallcontacts() { list<contact> contactlist = new arraylist<contact>(); // select query string selectquery = "select * contacts order name"; sqlitedatabase db = this.getwritabledatabase(); cursor cursor = db.rawquery(selectquery, null); // looping through rows , adding list if (cursor.movetofirst()) { { contact contact = new contact(); contact.setid(integer.parseint(cursor.getstring(0))); contact.setname(cursor.getstring(2)); contact.setprice(cursor.getstring(1)); contact.setimage(cursor.getblob(4)); contact.setsearch(cursor.getstring(3)); // adding contact list contactlist.add(contact); } while (cursor.movetonext()); } // close inserting data database db.close(); // return contact list return contactlist; } public cursor searchcustomer(string inputtext) throws sqlexception { log.w(tag, inputtext); string query = " select id _id," + key_name + "," + key_price + " " + table_contacts + " " + key_search + " '" + inputtext + "';"; log.w(tag, query); sqlitedatabase db = this.getwritabledatabase(); cursor mcursor = db.rawquery(query,null); if (mcursor != null) { mcursor.movetofirst(); } return mcursor; } // getting contacts count public int getcontactscount() { string countquery = "select * " + table_contacts; sqlitedatabase db = this.getreadabledatabase(); cursor cursor = db.rawquery(countquery, null); cursor.close(); // return count return cursor.getcount(); } }
this search function when search words, list view supposed show search result matches words. however, list view unable display search result. due data isn't database?
public class search extends activity implements searchview.onquerytextlistener, searchview.oncloselistener{ private listview mlistview; private searchview searchview; databasehandler db; private textview nametext; private textview pricetext; private simpleadapter customers; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.search); searchview = (searchview) findviewbyid(r.id.searchview); searchview.seticonifiedbydefault(false); searchview.setonquerytextlistener(this); searchview.setoncloselistener(this); mlistview = (listview) findviewbyid(r.id.list1); db = new databasehandler(this); } public boolean onquerytextchange(string newtext) { showresults(newtext + "*"); return false; } public boolean onquerytextsubmit(string query) { showresults(query + "*"); return false; } public boolean onclose() { showresults(""); return false; } private void showresults(string query) { cursor cursor = db.searchcustomer((query != null ? query.tostring() : "@@@@")); if (cursor == null) { // } else { // specify columns want display in result string[] = new string[] { databasehandler.key_name, databasehandler.key_price}; // specify corresponding layout elements want columns go int[] = new int[] { r.id.txttitle, r.id.txtprice}; system.out.println("hello1"); // create simple cursor adapter definitions , apply them listview simplecursoradapter customers = new simplecursoradapter(this,r.layout.result, cursor, from, to); mlistview.setadapter(customers); system.out.println("hell1"); // define on-click listener list items mlistview.setonitemclicklistener(new adapterview.onitemclicklistener() { public void onitemclick(adapterview<?> parent, view view, int position, long id) { // cursor, positioned corresponding row in result set cursor cursor = (cursor) mlistview.getitematposition(position); system.out.println("hel1"); // state's capital row in database. string name = cursor.getstring(cursor.getcolumnindexorthrow("name")); string price = cursor.getstring(cursor.getcolumnindexorthrow("price")); system.out.println("hello2"); //get references textviews nametext = (textview) findviewbyid(r.id.txttitles); pricetext = (textview) findviewbyid(r.id.txtprices); system.out.println("hello3"); // update parent class's textview nametext.settext(name); pricetext.settext(price); system.out.println("hello4"); searchview.setquery("", true); } }); } } }
Comments
Post a Comment