i have error in select single row in android -
i have problem when startup program
i want know how use arab_book() function tablebook class table class
package info.androidhive.slidingmenu; public class book { private int id; private string title; private string author; public book() {} public book(string title, string auther) { this.title = title; this.author = auther; } // ---- setter public void setid(int id){ this.id = id; } public void settitle(string title){ this.title = title; } public void setauthor(string author){ this.author = author; } // --- getter --- public int getid(){ return id; } public string gettitle(){ return title; } public string getauthor(){ return author; } public string tostring(){ return "book >> id:"+id+" | title:"+title+" | author:"+author; } }
and booktable
package info.androidhive.slidingmenu; import java.util.linkedlist; import java.util.list; import android.content.contentvalues; import android.content.context; import android.database.cursor; import android.database.sqlite.sqlitedatabase; import android.database.sqlite.sqliteopenhelper; import android.util.log; public class booktable extends sqliteopenhelper { private static final int db_version = 1; private static final string db_name = "book"; //book table name private static final string table_book = "books"; //book table columns name private static final string key_id = "id"; private static final string key_title = "title"; private static final string key_auther = "author"; private static final string[] columns = {key_id, key_title, key_auther}; public booktable(context context) { super(context, db_name, null, db_version); } @override public void oncreate(sqlitedatabase db) { // todo auto-generated method stub // sql statement create book table string create_book_table = "create table books ( " + "id integer primary key autoincrement, " + "title text, "+ "author text )"; // create books table db.execsql(create_book_table); } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { // todo auto-generated method stub db.execsql("drop table if exists books"); // create fresh books table this.oncreate(db); } /* * -------------------------------------------------------------------------------- */ public void addbook(book book) { log.d("addbook", book.tostring()); sqlitedatabase db = this.getwritabledatabase(); contentvalues values = new contentvalues(); values.put(key_title, book.gettitle()); values.put(key_auther, book.getauthor()); db.insert(table_book, null, values); db.close(); } public book arab_book(string id){ sqlitedatabase db = this.getreadabledatabase(); cursor c = db.query(table_book, columns, key_id, new string[] {id}, null, null, null); if( c != null ) c.movetofirst(); book book = new book(); book.setid(integer.parseint(c.getstring(0))); book.settitle(c.getstring(1)); book.setauthor(c.getstring(2)); return book; } public book getbook(int id) { sqlitedatabase db = this.getreadabledatabase(); cursor c = db.query(table_book, columns, key_id, new string[] {string.valueof(id)}, null, null, null, null); if( c != null ) c.movetofirst(); book book = new book(); book.setid(c.getint(c.getcolumnindex(key_id))); book.settitle((c.getstring(c.getcolumnindex(key_title)))); book.setauthor(c.getstring(c.getcolumnindex(key_auther))); //log.d("getbook("+id+")", book.tostring()); return book; } // books public book[] getallbooks() { book list[] = null; // 1. build query string query = "select * " + table_book; // 2. reference writable db sqlitedatabase db = this.getwritabledatabase(); cursor cursor = db.rawquery(query, null); system.out.println("count of data in book table >>"+cursor.getcount()); list = new book[cursor.getcount()]; // 3. go on each row, build book , add list book book = null; int index = 0; if (cursor.movetofirst()) { { book = new book(); book.setid(integer.parseint(cursor.getstring(0))); book.settitle(cursor.getstring(1)); book.setauthor(cursor.getstring(2)); // add book books list[index]= book; system.out.println("book "+index+": "); index++; } while (cursor.movetonext()); } /* //log.d("getallbooks()", books.tostring()); for(int i=0; i<list.length; i++){ system.out.println("book "+i+": "+list[i].gettitle()); }*/ // return books return list; } // updating single book public int updatebook(book book) { // 1. reference writable db sqlitedatabase db = this.getwritabledatabase(); // 2. create contentvalues add key "column"/value contentvalues values = new contentvalues(); values.put("title", book.gettitle()); // title values.put("author", book.getauthor()); // author // 3. updating row int = db.update(table_book, //table values, // column/value key_id+" = ?", // selections new string[] { string.valueof(book.getid()) }); //selection args // 4. close db.close(); return i; } // deleting single book public void deletebook(book book) { // 1. reference writable db sqlitedatabase db = this.getwritabledatabase(); // 2. delete db.delete(table_book, key_id+" = ?", new string[] { string.valueof(book.getid()) }); // 3. close db.close(); log.d("deletebook", book.tostring()); } }
and adapter class
package info.androidhive.slidingmenu; import android.app.activity; import android.content.context; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.baseadapter; import android.widget.imageview; import android.widget.textview; public class customlistviewadapter extends baseadapter { private activity activity; private book[] data; private static layoutinflater inflater=null; public customlistviewadapter(activity a, book list[]) { activity = a; data=list; inflater = (layoutinflater)activity.getsystemservice(context.layout_inflater_service); } public int getbookid(int position){ return data[position].getid(); } public int getcount() { return data.length; } public object getitem(int position) { return position; } public long getitemid(int position) { return position; } public view getview(int position, view convertview, viewgroup parent) { view vi=convertview; if(convertview==null) vi = inflater.inflate(r.layout.list_row, null); textview row_id =(textview)vi.findviewbyid(r.id.row_id); textview name=(textview)vi.findviewbyid(r.id.title); textview descp = (textview) vi.findviewbyid(r.id.artist); imageview image=(imageview)vi.findviewbyid(r.id.image); row_id.settext(string.valueof(data[position].getid())); name.settext(data[position].gettitle()); descp.settext("author: "+data[position].getauthor()); return vi; } }
and when use code in main activity have error
bt = new booktable(getactivity().getapplicationcontext()); book bs = bt.arab_book("4");
your call query method wrong. try read documentation of sqlitedatabase.query method. third parameter selection
, fourth selectionargs
.
all elements in selectionargs
-array replace corresponding question marks (?) in selection
-parameter.
try change code in getbook()
, arab_book()
this:
cursor c = db.query( table_book, //table columns, //projection (or columns) key_id + "=?", //my where-clause new string[]{string.valueof(id)}, //arguments inside where-claus null, //groupby null, //having null, //orderby null); //limit
but in general, should try read on basics of using database in android. consider using cursorloader , cursoradapter, reading content provider. can find more information in this tutorial.
note: might not problem. please post logcat-output provide more details.
Comments
Post a Comment