Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.60 KB | None | 0 0
  1. public class DatabaseHandler extends SQLiteOpenHelper {
  2.  
  3. private static final int DATABASE_VERSION = 1;
  4. protected static final String DATABASE_NAME = "wordDatabase";
  5.  
  6. public DatabaseHandler(Context context) {
  7. super(context, DATABASE_NAME, null, DATABASE_VERSION);
  8. }
  9.  
  10. @Override
  11. public void onCreate(SQLiteDatabase db) {
  12.  
  13. String sql = "CREATE TABLE words " +
  14. "( id INTEGER PRIMARY KEY AUTOINCREMENT, " +
  15. "word TEXT, " +
  16. "meaning TEXT, " +
  17. "details TEXT, " +
  18. "lesson TEXT, " +
  19. "ticks INTEGER ) ";
  20. db.execSQL(sql);
  21.  
  22. }
  23.  
  24. @Override
  25. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  26.  
  27. String sql = "DROP TABLE IF EXISTS words";
  28. db.execSQL(sql);
  29. onCreate(db);
  30. }
  31.  
  32. public class ObjectStudent {
  33.  
  34. int id;
  35. String word;
  36. String meaning;
  37. String details;
  38. String lesson;
  39. int ticks;
  40.  
  41. public ObjectStudent(){
  42.  
  43. }
  44.  
  45. public class OnClickeListenerCreateStudent implements View.OnClickListener {
  46. @Override
  47. public void onClick(View view) {
  48. final Context context = view.getRootView().getContext();
  49.  
  50. LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  51. final View formElementsView = inflater.inflate(R.layout.student_input_form, null, false);
  52.  
  53. final EditText et_word = (EditText) formElementsView.findViewById(R.id.et_word);
  54. final EditText et_meaning = (EditText) formElementsView.findViewById(R.id.et_meaning);
  55. final EditText et_details = (EditText) formElementsView.findViewById(R.id.et_details);
  56. final EditText et_lesson = (EditText) formElementsView.findViewById(R.id.et_lesson);
  57.  
  58.  
  59. new AlertDialog.Builder(context)
  60. .setView(formElementsView)
  61. .setTitle("Create Word")
  62. .setPositiveButton("Add",
  63. new DialogInterface.OnClickListener() {
  64. public void onClick(DialogInterface dialog, int id) {
  65. String wordTitle = et_word.getText().toString();
  66. String wordMeaning = et_meaning.getText().toString();
  67. String wordDetails = et_details.getText().toString();
  68. String wordLesson = et_lesson.getText().toString();
  69.  
  70. ObjectStudent objectStudent = new ObjectStudent();
  71. /* objectStudent.word= wordTitle;
  72. objectStudent.meaning= wordMeaning;
  73. objectStudent.details= wordDetails;
  74. objectStudent.lesson= wordLesson;*/
  75. objectStudent.word = "word"; // et_word.getText().toString();
  76. objectStudent.meaning = "meaning"; // et_meaning.getText().toString();
  77. objectStudent.details = "details"; // et_details.getText().toString();
  78. objectStudent.lesson = "lesson"; // et_lesson.getText().toString();
  79.  
  80. objectStudent.ticks= 1;
  81.  
  82. boolean createSuccessful = new TableControllerStudent(context).create(objectStudent);
  83.  
  84. if(createSuccessful){
  85. Toast.makeText(context, "Word information was saved.", Toast.LENGTH_SHORT).show();
  86. }else{
  87. Toast.makeText(context, "Unable to save Word information.", Toast.LENGTH_SHORT).show();
  88. }
  89.  
  90. ((MainActivity) context).countRecords();
  91. ((MainActivity) context).readRecords();
  92.  
  93.  
  94.  
  95. dialog.cancel();
  96. }
  97.  
  98. }).show();
  99. }
  100.  
  101. public class TableControllerStudent extends DatabaseHandler {
  102.  
  103. public TableControllerStudent(Context context) {
  104. super(context);
  105. }
  106.  
  107. public boolean create(ObjectStudent objectStudent) {
  108.  
  109. ContentValues values = new ContentValues();
  110.  
  111.  
  112. /* values.put("word", objectStudent.word);
  113. values.put("meaning", objectStudent.meaning);
  114. values.put("details", objectStudent.details);
  115. values.put("lesson", objectStudent.lesson);*/
  116.  
  117. values.put("word", "w");
  118. values.put("meaning", "m");
  119. values.put("details", "d");
  120. values.put("lesson", "l");
  121.  
  122. values.put("ticks", objectStudent.ticks);
  123.  
  124. SQLiteDatabase db = this.getWritableDatabase();
  125.  
  126. db.execSQL("INSERT INTO " + "words "+ "(word, meaning,details, lesson, ticks ) VALUES ('word','meaning','details','lesson',2)");
  127.  
  128. //boolean createSuccessful = db.insert("words", null, values) > 0;
  129. db.close();
  130.  
  131. //return createSuccessful;
  132. return true;
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement