Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.62 KB | None | 0 0
  1. public class DatabaseHelper extends SQLiteOpenHelper {
  2.  
  3. private static final String DB_NAME = "mydic.db";
  4. private static final int DB_VERSION = 1;
  5. private String DB_PATH = null;
  6. private static final String TABLE_DICTIONARY = "dictionary1";
  7. public static final String TABLE_BOOKMARK= "bookmark";
  8. private static final String COLUMN_ID = "id";
  9. public static final String COL_WORD = "word";
  10. public static final String COL_DEFINITION = "definition";
  11. private Context mcontext;
  12. private SQLiteDatabase mDatabase;
  13.  
  14. public DatabaseHelper( Context context) {
  15. super(context, DB_NAME, null, DB_VERSION);
  16. this.mcontext = context;
  17. this.DB_PATH = "/data/data/" + context.getPackageName() + "/" + "databases/";
  18. }
  19. public void createDataBase() throws IOException {
  20.  
  21. boolean dbExist = checkDataBase();
  22. if (!dbExist) {
  23. this.getReadableDatabase();
  24. try {
  25. mcontext.deleteDatabase(DB_NAME);
  26. copyDataBase();
  27. } catch (IOException e) {
  28. throw new Error("Error copying database");
  29. }
  30. }
  31. }
  32. public boolean checkDataBase() {
  33. SQLiteDatabase checkDB = null;
  34. try {
  35. String myPath = DB_PATH + DB_NAME;
  36. checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
  37. } catch (SQLiteException e) {
  38. e.printStackTrace();
  39. }
  40. if (checkDB != null) {
  41. checkDB.close();
  42. }
  43. return checkDB != null ? true : false;
  44. }
  45.  
  46. private void copyDataBase() throws IOException {
  47.  
  48. InputStream myInput = mcontext.getAssets().open(DB_NAME);
  49. String outFileName = DB_PATH + DB_NAME;
  50. OutputStream myOutput = new FileOutputStream(outFileName);
  51. byte[] buffer = new byte[1024];
  52. int length;
  53. while ((length = myInput.read(buffer)) > 0) {
  54. myOutput.write(buffer, 0, length);
  55. }
  56. myOutput.flush();
  57. myOutput.close();
  58. myInput.close();
  59. }
  60. public void openDatabase() throws SQLException {
  61. String myPath = DB_PATH + DB_NAME;
  62. mDatabase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
  63. }
  64.  
  65. @Override
  66. public synchronized void close() {
  67. if (mDatabase != null)
  68. mDatabase.close();
  69. super.close();
  70. }
  71. public Cursor getListWord(String wordSearch) {
  72.  
  73. String query = "SELECT * FROM " + TABLE_DICTIONARY + " Where word Like ? " ;
  74. Cursor cursor = null;
  75. try {
  76. openDatabase();
  77. String[] args = {"%"+wordSearch+"%"};
  78. cursor = mDatabase.rawQuery(query , args);
  79. }catch (Exception e){
  80. e.printStackTrace();
  81. }
  82. return cursor;
  83. }
  84. public boolean addBookmark(String title, String subtitle) {
  85.  
  86. // Gets the data repository in write mode
  87. SQLiteDatabase db = getWritableDatabase();
  88.  
  89. // Create a new map of values, where column names are the keys
  90. ContentValues values = new ContentValues();
  91. values.put(DatabaseHelper.COL_WORD, title);
  92. values.put(DatabaseHelper.COL_DEFINITION, subtitle);
  93.  
  94. // Insert the new row, returning the primary key value of the new row
  95. db.insert(DatabaseHelper.TABLE_BOOKMARK, null, values);
  96.  
  97. return true;
  98. }
  99. public void removeBookmark(String title, String subtitle) {
  100. mDatabase = this.getReadableDatabase();
  101. mDatabase.execSQL("delete from favourites where word Like ? ");
  102. mDatabase.close();
  103. }
  104. public Cursor getBookmarkWord(String wordSearch) {
  105. String query = "SELECT * FROM favourites Where word Like ? " ;
  106. Cursor cursor = null;
  107. try {
  108. openDatabase();
  109. String[] args = {"%"+wordSearch+"%"};
  110. cursor = mDatabase.rawQuery(query , args);
  111. }catch (Exception e){
  112. e.printStackTrace();
  113. }
  114. return cursor;
  115. }
  116.  
  117. @Override
  118. public void onCreate(SQLiteDatabase db) {
  119. db.execSQL("CREATE TABLE favourites ( id INTEGER PRIMARY KEY AUTOINCREMENT,word,definition)" );
  120. }
  121.  
  122. @Override
  123. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  124.  
  125. if (newVersion > oldVersion) {
  126. try {
  127. copyDataBase();
  128. } catch (IOException e) {
  129. e.printStackTrace();
  130. }
  131. }
  132. }
  133.  
  134. word = itemView.findViewById(R.id.wordText);
  135. itemView.setOnClickListener(new View.OnClickListener() {
  136. @Override
  137. public void onClick(View v) {
  138.  
  139. int position = getAdapterPosition();
  140. cursor.moveToPosition(position);
  141.  
  142. Intent intent = new Intent(context, DetailActivity.class);
  143. intent.putExtra("WORD",cursor.getString(1));
  144. intent.putExtra("DEFINITION",cursor.getString(2));
  145. context.startActivity(intent);
  146.  
  147. final String word = getIntent().getStringExtra("WORD");
  148. final String definition = getIntent().getStringExtra("DEFINITION");
  149.  
  150.  
  151. final TextView tvWordDetail = findViewById(R.id.tvWordDetail);
  152. WebView wordDefinition = findViewById(R.id.tvWordDefinition);
  153. final ImageButton btnBookmark = findViewById(R.id.btnBookmark);
  154. ImageButton textToSpeech = findViewById(R.id.textToSpeech);
  155.  
  156.  
  157. tvWordDetail.setText(Html.fromHtml(word));
  158. wordDefinition.loadDataWithBaseURL(null,definition,"text/html", "utf-8",null);
  159.  
  160. Cursor bookmarkWord = mDBHelper.getBookmarkWord("");
  161. int isMark = bookmarkWord == null? 0 : 1;
  162. btnBookmark.setTag(isMark);
  163.  
  164. int icon = bookmarkWord == null? R.drawable.ic_bookmark_border:R.drawable.ic_bookmark_fill;
  165. btnBookmark.setImageResource(icon);
  166. btnBookmark.setOnClickListener(new View.OnClickListener() {
  167. @Override
  168. public void onClick(View v) {
  169.  
  170. int i = (int) btnBookmark.getTag();
  171. if (i == 0) {
  172. btnBookmark.setBackgroundResource(R.drawable.ic_bookmark_fill);
  173. btnBookmark.setTag(1);
  174. mDBHelper.addBookmark(word,definition);
  175. } else if (i == 1){
  176. btnBookmark.setBackgroundResource(R.drawable.ic_bookmark_border);
  177. btnBookmark.setTag(0);
  178. mDBHelper.removeBookmark(word,definition);
  179. }
  180.  
  181.  
  182. }
  183. });
  184.  
  185. 2019-02-19 15:08:08.727 11561-11561/com.elytelabs.testnav E/AndroidRuntime: FATAL EXCEPTION: main
  186. Process: com.elytelabs.testnav, PID: 11561
  187. java.lang.RuntimeException: Unable to start activity ComponentInfo{com.elytelabs.testnav/com.elytelabs.testnav.DetailActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.Cursor com.elytelabs.testnav.database.DatabaseHelper.getBookmarkWord(java.lang.String)' on a null object reference
  188. at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
  189. at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
  190. at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
  191. at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
  192. at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
  193. at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
  194. at android.os.Handler.dispatchMessage(Handler.java:106)
  195. at android.os.Looper.loop(Looper.java:193)
  196. at android.app.ActivityThread.main(ActivityThread.java:6669)
  197. at java.lang.reflect.Method.invoke(Native Method)
  198. at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
  199. at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
  200. Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.Cursor com.elytelabs.testnav.database.DatabaseHelper.getBookmarkWord(java.lang.String)' on a null object reference
  201. at com.elytelabs.testnav.DetailActivity.onCreate(DetailActivity.java:54)
  202. at android.app.Activity.performCreate(Activity.java:7136)
  203. at android.app.Activity.performCreate(Activity.java:7127)
  204. at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
  205. at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
  206. at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048) 
  207. at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
  208. at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
  209. at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
  210. at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808) 
  211. at android.os.Handler.dispatchMessage(Handler.java:106) 
  212. at android.os.Looper.loop(Looper.java:193) 
  213. at android.app.ActivityThread.main(ActivityThread.java:6669) 
  214. at java.lang.reflect.Method.invoke(Native Method) 
  215. at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
  216. at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement