java - how to separate [String] by "-" and make multiple column on android? -
i'm working on searchable dictionary. raw database separated "-" like
columna(key_word) columnb(key_defination) apple - 1 kind of fruit mango - 1 fruit
but want make 3 column, (column c key_details)
columna columnb columnc (key_details) apple - 1 kind of fruit - round shape mango - 1 fruit - found in bangladesh
when people search a, application show column b , c. reason why splited string want show coumn b , c on different window. how that? here source-
/** * contains logic return specific words dictionary, , * load dictionary table when needs created. */ public class dictionarydatabase { private static final string tag = "dictionarydatabase"; //the columns we'll include in dictionary table public static final string key_word = searchmanager.suggest_column_text_1; public static final string key_definition = searchmanager.suggest_column_text_2; private static final string database_name = "dictionary"; private static final string fts_virtual_table = "ftsdictionary"; private static final int database_version = 2; private final dictionaryopenhelper mdatabaseopenhelper; private static final hashmap<string,string> mcolumnmap = buildcolumnmap(); public dictionarydatabase(context context) { mdatabaseopenhelper = new dictionaryopenhelper(context); } private static hashmap<string,string> buildcolumnmap() { hashmap<string,string> map = new hashmap<string,string>(); map.put(key_word, key_word); map.put(key_definition, key_definition); map.put(basecolumns._id, "rowid " + basecolumns._id); map.put(searchmanager.suggest_column_intent_data_id, "rowid " + searchmanager.suggest_column_intent_data_id); map.put(searchmanager.suggest_column_shortcut_id, "rowid " + searchmanager.suggest_column_shortcut_id); return map; } public cursor getword(string rowid, string[] columns) { string selection = "rowid = ?"; string[] selectionargs = new string[] {rowid}; return query(selection, selectionargs, columns); } public cursor getwordmatches(string query, string[] columns) { string selection = key_word + " match ?"; string[] selectionargs = new string[] {query+"*"}; return query(selection, selectionargs, columns); } private cursor query(string selection, string[] selectionargs, string[] columns) { sqlitequerybuilder builder = new sqlitequerybuilder(); builder.settables(fts_virtual_table); builder.setprojectionmap(mcolumnmap); cursor cursor = builder.query(mdatabaseopenhelper.getreadabledatabase(), columns, selection, selectionargs, null, null, null); if (cursor == null) { return null; } else if (!cursor.movetofirst()) { cursor.close(); return null; } return cursor; } private static class dictionaryopenhelper extends sqliteopenhelper { private final context mhelpercontext; private sqlitedatabase mdatabase; private static final string fts_table_create = "create virtual table " + fts_virtual_table + " using fts3 (" + key_word + ", " + key_definition + ");"; dictionaryopenhelper(context context) { super(context, database_name, null, database_version); mhelpercontext = context; } @override public void oncreate(sqlitedatabase db) { mdatabase = db; mdatabase.execsql(fts_table_create); loaddictionary(); } private void loaddictionary() { new thread(new runnable() { public void run() { try { loadwords(); } catch (ioexception e) { throw new runtimeexception(e); } } }).start(); } private void loadwords() throws ioexception { log.d(tag, "loading words..."); final resources resources = mhelpercontext.getresources(); inputstream inputstream = resources.openrawresource(r.raw.definitions); bufferedreader reader = new bufferedreader(new inputstreamreader(inputstream)); try { string line; while ((line = reader.readline()) != null) { string[] strings = textutils.split(line, "-"); if (strings.length < 2) continue; long id = addword(strings[0].trim(), strings[1].trim()); if (id < 0) { log.e(tag, "unable add word: " + strings[0].trim()); } } } { reader.close(); } log.d(tag, "done loading words."); } public long addword(string word, string definition) { contentvalues initialvalues = new contentvalues(); initialvalues.put(key_word, word); initialvalues.put(key_definition, definition); return mdatabase.insert(fts_virtual_table, null, initialvalues); } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { log.w(tag, "upgrading database version " + oldversion + " " + newversion + ", destroy old data"); db.execsql("drop table if exists " + fts_virtual_table); oncreate(db); } } }
string[] arr = string.split("-");
returns array of strings computed splitting string around matches of given regular expression.
edit:
long id = addword(strings[0].trim(), strings[1].trim());
above line should be:
long id = addword(strings[0].trim(), strings[1].trim(),strings[2].trim());
and change addword function mentioned below.
public long addword(string word, string definition,string details) { contentvalues initialvalues = new contentvalues(); initialvalues.put(key_word, word); initialvalues.put(key_definition, definition); initialvalues.put(key_details, details); return mdatabase.insert(fts_virtual_table, null, initialvalues); }
Comments
Post a Comment