android - New Line in my sqlite column -
hi, guys! first of all, i'm newbie. have column in database called definition
. in column there many sentences. line break (paragraph) after each sentence. don't know how this. tried type \n after sentence, nothing happens. me?
there source code:
dictionarydatahelper
public class dictionarydatabasehelper extends sqliteopenhelper { final static string dictionary_database="dictionary"; final static string item_id_column="id"; final static string word_column="word"; final static string definition_column="definition"; final static string create_database_query="create table "+dictionary_database+" ( "+ item_id_column+" integer primary key autoincrement, "+ word_column+" text , "+ definition_column+" text)"; final static string on_upgrade_query="drop table "+dictionary_database; context context; public dictionarydatabasehelper(context context, string name, cursorfactory factory, int version) { super(context, dictionary_database, factory, version); this.context=context; } @override public void oncreate(sqlitedatabase database) { database.execsql(create_database_query); } @override public void onupgrade(sqlitedatabase database, int oldversion, int newversion) { database.execsql(on_upgrade_query); oncreate(database); } public long insertdata(worddefinition worddefinition) { sqlitedatabase database=this.getwritabledatabase(); contentvalues values=new contentvalues(); values.put(word_column, worddefinition.word); values.put(definition_column, worddefinition.definition); return database.insert(dictionary_database, null, values); } public long updatedata(worddefinition worddefinition) { sqlitedatabase database=this.getwritabledatabase(); contentvalues values=new contentvalues(); values.put(word_column, worddefinition.word); values.put(definition_column, worddefinition.definition); return database.update(dictionary_database, values, word_column+" =?", new string[]{worddefinition.word}); } public void deletedata(worddefinition worddefinition) { sqlitedatabase database=this.getwritabledatabase(); string querystring="delete "+dictionary_database+" "+word_column+" = '"+worddefinition.word+"'"; database.execsql(querystring); } public arraylist<worddefinition> getallwords() { arraylist<worddefinition> arraylist=new arraylist<worddefinition>(); sqlitedatabase database=this.getreadabledatabase(); string selectallquerystring="select * "+dictionary_database; cursor cursor=database.rawquery(selectallquerystring, null); if (cursor.movetofirst()) { { worddefinition worddefinition=new worddefinition(cursor.getstring(cursor.getcolumnindex(word_column)), cursor.getstring(cursor.getcolumnindex(definition_column))); arraylist.add(worddefinition); } while (cursor.movetonext()); } return arraylist; } public worddefinition getworddefinition(string word) { sqlitedatabase database=this.getreadabledatabase(); worddefinition worddefinition=null; string selectquerystring="select * "+dictionary_database+ " "+word_column+" = '"+word+ "'"; cursor cursor=database.rawquery(selectquerystring, null); if (cursor.movetofirst()) { worddefinition=new worddefinition(cursor.getstring(cursor.getcolumnindex(word_column)), cursor.getstring(cursor.getcolumnindex(definition_column))); } return worddefinition; } public worddefinition getworddefinition(long id) { sqlitedatabase database=this.getreadabledatabase(); worddefinition worddefinition=null; string selectquerystring="select * "+dictionary_database+ " "+item_id_column+" = '"+id+ "'"; cursor cursor=database.rawquery(selectquerystring, null); if (cursor.movetofirst()) { worddefinition=new worddefinition(cursor.getstring(cursor.getcolumnindex(word_column)), cursor.getstring(cursor.getcolumnindex(definition_column))); } return worddefinition; } public void initializedatabaseforthefirsttime(arraylist<worddefinition> worddefinitions) { sqlitedatabase database=this.getwritabledatabase(); database.execsql("begin"); contentvalues contentvalues=new contentvalues(); (worddefinition worddefinition : worddefinitions) { contentvalues.put(word_column, worddefinition.word); contentvalues.put(definition_column, worddefinition.definition); database.insert(dictionary_database, null, contentvalues); } database.execsql("commit"); } }
dictionarylistactivity
public class dictionarylistactivity extends activity { textview usertextview; edittext searchedittext; button searchbutton; listview dictionarylistview; string logtagstring="dictionary"; arraylist<worddefinition> allworddefinitions=new arraylist<worddefinition>(); dictionarydatabasehelper mydictionarydatabasehelper; sharedpreferences sharedpreferences; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_dictionary_list); log.d("dictionary", "second activity started"); usertextview=(textview) findviewbyid(r.id.persontextview); usertextview.settext(getintent().getstringextra(mainactivity.user_name_string)); searchedittext=(edittext) findviewbyid(r.id.searchedittext); searchbutton=(button) findviewbyid(r.id.searchbutton); dictionarylistview=(listview) findviewbyid(r.id.dictionarylistview); mydictionarydatabasehelper=new dictionarydatabasehelper(this, "dictionary", null, 1); sharedpreferences=getsharedpreferences(mainactivity.shared_name_string, mode_private); boolean initialized=sharedpreferences.getboolean("initialized", false); if (initialized==false) { //log.d(logtagstring, "initializing first time"); initializedatabase(); sharedpreferences.editor editor=sharedpreferences.edit(); editor.putboolean("initialized", true); editor.commit(); }else { log.d(logtagstring, "db initialized"); } allworddefinitions=mydictionarydatabasehelper.getallwords(); dictionarylistview.setadapter(new baseadapter() { @override public view getview(int position, view view, viewgroup arg2) { if (view==null) { view=getlayoutinflater().inflate(r.layout.list_item, null); } textview textview=(textview) view.findviewbyid(r.id.listitemtextview); textview.settext(allworddefinitions.get(position).word); return view; } @override public long getitemid(int arg0) { // todo auto-generated method stub return 0; } @override public object getitem(int arg0) { // todo auto-generated method stub return null; } @override public int getcount() { // todo auto-generated method stub return allworddefinitions.size(); } }); dictionarylistview.setonitemclicklistener(new onitemclicklistener() { @override public void onitemclick(adapterview<?> arg0, view view, int position, long arg3) { intent intent =new intent(dictionarylistactivity.this, worddefinitiondetailactivity.class); intent.putextra("word", allworddefinitions.get(position).word); intent.putextra("definition", allworddefinitions.get(position).definition); startactivity(intent); } }); searchbutton.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { string string=searchedittext.gettext().tostring(); worddefinition worddefinition=mydictionarydatabasehelper.getworddefinition(string); if (worddefinition==null) { toast.maketext(dictionarylistactivity.this, "expressão não encontrada.", toast.length_long).show(); }else { intent intent =new intent(dictionarylistactivity.this, worddefinitiondetailactivity.class); intent.putextra("word", worddefinition.word); intent.putextra("definition", worddefinition.definition); startactivity(intent); } } }); } // private void initializedatabase() { inputstream inputstream=getresources().openrawresource(r.raw.dictionary); bufferedreader bufferedreader=new bufferedreader(new inputstreamreader(inputstream)); dictionaryloader.loaddata(bufferedreader, mydictionarydatabasehelper); } }
dictionaryloader
public class dictionaryloader { public static void loaddata(bufferedreader bufferedreader, dictionarydatabasehelper dictionarydatabasehelper) { arraylist<worddefinition> allwords=new arraylist<worddefinition>(); try { bufferedreader filereader=bufferedreader; try { int c=17; c=filereader.read(); while (c!=(-1)) { stringbuilder stringbuilder=new stringbuilder(); while ((char)c!='\n'&&c!=-1) { try { stringbuilder.append((char)c); } catch (exception e) { // todo auto-generated catch block system.out.println(stringbuilder.length()); //e.printstacktrace(); } c= filereader.read(); if (c==-1) { return; } } string wordstring=stringbuilder.tostring(); arraylist<string> definition=new arraylist<string>(); while (c=='\n'||c=='\t') { c= filereader.read(); if (c=='\n'||c=='\t'||c=='\r') { stringbuilder stringbuilder2=new stringbuilder(); while (c!='\n') { stringbuilder2.append((char)c); c=filereader.read(); } string definitionstring=stringbuilder2.tostring(); definition.add(definitionstring); }else { break; } } wordstring=wordstring.trim(); //logger.log("word loaded: "+(++counter)+" :"+wordstring); allwords.add(new worddefinition(wordstring, definition)); } } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } try { dictionarydatabasehelper.initializedatabaseforthefirsttime(allwords); filereader.close(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } catch (exception e) { // todo auto-generated catch block e.printstacktrace(); } }
}
if inserting contentvalues
, strings can contain newline character (\n
) , insert fine. far can see, code loads words never adds character strings builds.
also, should not executing begin
, commit
statements that. proper way looks this:
sqlitedb.begintransaction(); try { // database operations here sqlitedb.settransactionsuccessful(); } { sqlitedb.endtransaction(); }
Comments
Post a Comment