Guest User

Untitled

a guest
Dec 12th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.98 KB | None | 0 0
  1. public class DatabaseTable {
  2.  
  3. private static final String TAG = "DictionaryDatabase";
  4.  
  5. //The columns we'll include in the dictionary table
  6. public static final String COL_WORD = "WORD";
  7. public static final String COL_DEFINITION = "DEFINITION";
  8. public static final String COL_PRONUNCIATION = "PRONUNCIATION";
  9. public static final String COL_EXAMPLE = "EXAMPLE";
  10. public static final String COL_SYNONYMS = "SYNONYMS";
  11. public static final String COL_COMMON_MEAN = "COMMON_MEAN";
  12. public static final String COL_MORE_MEAN = "MORE_MEAN";
  13.  
  14. private static final String DATABASE_NAME = "DICTIONARY";
  15. private static final String FTS_VIRTUAL_TABLE = "FTS";
  16. private static final int DATABASE_VERSION = 1;
  17.  
  18. public final DatabaseOpenHelper mDatabaseOpenHelper;
  19.  
  20. public DatabaseTable(Context context) {
  21. mDatabaseOpenHelper = new DatabaseOpenHelper(context);
  22. }
  23.  
  24. public static class DatabaseOpenHelper extends SQLiteOpenHelper {
  25.  
  26. private final Context mHelperContext;
  27. private SQLiteDatabase mDatabase;
  28.  
  29. private static final String FTS_TABLE_CREATE =
  30. "CREATE VIRTUAL TABLE " + FTS_VIRTUAL_TABLE +
  31. " USING fts4 (" +
  32. COL_WORD + ", " +
  33. COL_COMMON_MEAN + ", " +
  34. COL_PRONUNCIATION + ", " +
  35. COL_MORE_MEAN + ", " +
  36. COL_DEFINITION + ", " +
  37. COL_SYNONYMS + ", " +
  38. COL_EXAMPLE + ")";
  39.  
  40. DatabaseOpenHelper(Context context) {
  41. super(context, DATABASE_NAME, null, DATABASE_VERSION);
  42. mHelperContext = context;
  43. }
  44.  
  45. @Override
  46. public void onCreate(SQLiteDatabase db) {
  47. mDatabase = db;
  48. mDatabase.execSQL(FTS_TABLE_CREATE);
  49.  
  50. loadDictionary();
  51.  
  52. }
  53.  
  54. @Override
  55. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  56. Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
  57. + newVersion + ", which will destroy all old data");
  58. db.execSQL("DROP TABLE IF EXISTS " + FTS_VIRTUAL_TABLE);
  59. onCreate(db);
  60. }
  61.  
  62.  
  63. private void loadDictionary() {
  64. new Thread(new Runnable() {
  65. public void run() {
  66. try {
  67. loadWords();
  68. } catch (IOException e) {
  69. throw new RuntimeException(e);
  70. }
  71. }
  72. }).start();
  73. }
  74.  
  75. private void loadWords() throws IOException {
  76. final Resources resources = mHelperContext.getResources();
  77. //InputStream inputStream = resources.openRawResource();
  78. BufferedReader reader = new BufferedReader(new InputStreamReader(mHelperContext.getAssets().open("definition1.csv"), "UTF-8"));
  79.  
  80. try {
  81. String line;
  82. while ((line = reader.readLine()) != null) {
  83. String[] strings = TextUtils.split(line, "\|");
  84. if (strings.length != 7) {
  85. Log.d("CSVParser", "Skipping Bad CSV Row");
  86. continue;
  87. }
  88. long id = addWord(strings[0].trim(), strings[1].trim(), strings[2].trim(), strings[3].trim(), strings[4].trim(), strings[5].trim(), strings[6].trim());
  89. if (id < 0) {
  90. Log.e(TAG, "unable to add word: " + strings[0].trim());
  91. }
  92. }
  93. } finally {
  94. reader.close();
  95. }
  96. }
  97.  
  98. public long addWord(String word, String common_mean, String pronuntiation, String more_mean, String definition, String synonym, String example) {
  99. ContentValues initialValues = new ContentValues();
  100. initialValues.put(COL_WORD, word);
  101. initialValues.put(COL_COMMON_MEAN, common_mean);
  102. initialValues.put(COL_PRONUNCIATION, pronuntiation);
  103. initialValues.put(COL_MORE_MEAN, more_mean);
  104. initialValues.put(COL_DEFINITION, definition);
  105. initialValues.put(COL_SYNONYMS, synonym);
  106. initialValues.put(COL_EXAMPLE, example);
  107. Log.d("inserting", word);
  108.  
  109. return mDatabase.insert(FTS_VIRTUAL_TABLE, null, initialValues);
  110. }
  111.  
  112. public Cursor getWordMatches(String query, String[] columns) {
  113. String selection = COL_WORD + " MATCH ?";
  114. String[] selectionArgs = new String[]{query + "ORDER BY rank"};
  115.  
  116. return query(selection, selectionArgs, columns);
  117. }
  118.  
  119. private Cursor query(String selection, String[] selectionArgs, String[] columns) {
  120. SQLiteQueryBuilder builder = new SQLiteQueryBuilder();
  121. builder.setTables(FTS_VIRTUAL_TABLE);
  122.  
  123. Cursor cursor = builder.query(getReadableDatabase(),
  124. columns, selection, selectionArgs, null, null, null);
  125.  
  126. if (cursor == null) {
  127. return null;
  128. } else if (!cursor.moveToFirst()) {
  129. cursor.close();
  130. return null;
  131. }
  132. return cursor;
  133. }
  134.  
  135. private SearchView.OnQueryTextListener onQueryTextListener =
  136. new SearchView.OnQueryTextListener() {
  137. @Override
  138. public boolean onQueryTextSubmit(String query) {
  139.  
  140. Toast.makeText(MainActivity.this, "Problem in Fetching Deals",
  141. Toast.LENGTH_LONG).show();
  142. return true;
  143. }
  144.  
  145. @Override
  146. public boolean onQueryTextChange(String newText) {
  147. getDealsFromDb(newText);
  148. return true;
  149. }
  150.  
  151. private void getDealsFromDb(final String searchText) {
  152.  
  153. runOnUiThread(new Runnable() {
  154. @Override
  155. public void run() {
  156.  
  157. deals=databaseTable.mDatabaseOpenHelper.getallword(searchText);
  158. SuggestionAdapter1 adapter=new SuggestionAdapter1
  159. (MainActivity.this, deals, R.layout.suggestion_item_layout);
  160. listView.setAdapter(adapter);
  161.  
  162.  
  163. }
  164. });
  165.  
  166.  
  167.  
  168.  
  169. }
  170. };
  171.  
  172. public class SuggestionAdapter1 extends ArrayAdapter{
  173. private List<ItemObject> dataList;
  174. private Context mContext;
  175. private int searchResultItemLayout;
  176.  
  177. public SuggestionAdapter1(Context context, List<ItemObject> resource,
  178. int rid) {
  179. super(context, rid, resource);
  180. dataList = resource;
  181. mContext = context;
  182. searchResultItemLayout = rid;
  183. }
  184.  
  185. @Override
  186. public int getCount() {
  187. return dataList.size();
  188. }
  189.  
  190. @Override
  191. public ItemObject getItem(int position) {
  192. return dataList.get(position);
  193. }
  194.  
  195. @Override
  196. public View getView(int position, View view, @NonNull ViewGroup parent) {
  197.  
  198. if (view == null) {
  199. view = LayoutInflater.from(parent.getContext())
  200. .inflate(searchResultItemLayout, parent, false);
  201. }
  202.  
  203. ItemObject di = getItem(position);
  204.  
  205. TextView dealsTv = (TextView) view.findViewById(R.id.eng);
  206. dealsTv.setText(di.getId());
  207.  
  208. TextView cashbackTv = (TextView) view.findViewById(R.id.bng);
  209. cashbackTv.setText(di.getWord());
  210.  
  211.  
  212.  
  213.  
  214. return view;
  215. }
  216.  
  217. }
Add Comment
Please, Sign In to add comment