· 8 years ago · Jul 27, 2018, 05:16 PM
1ANdroid SQLite not working in separate classes
2public class DataBaseHelper extends SQLiteOpenHelper {
3
4public static final String TABLE_COMMENTS = "comments";
5public static final String COLUMN_ID = "_id";
6public static final String COLUMN_COMMENT = "comment";
7
8private static final String DATABASE_NAME = "commments.db";
9private static final int DATABASE_VERSION = 1;
10
11// Database creation sql statement
12private static final String DATABASE_CREATE = "create table "
13 + TABLE_COMMENTS + "( " + COLUMN_ID
14 + " integer primary key autoincrement, " + COLUMN_COMMENT
15 + " text not null);";
16
17public DataBaseHelper(Context context) {
18 super(context, DATABASE_NAME, null, DATABASE_VERSION);
19}
20
21@Override
22public void onCreate(SQLiteDatabase database) {
23 database.execSQL(DATABASE_CREATE);
24}
25
26@Override
27public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
28 Log.w(DataBaseHelper.class.getName(),
29 "Upgrading database from version " + oldVersion + " to "
30 + newVersion + ", which will destroy all old data");
31 db.execSQL("DROP TABLE IF EXISTS " + TABLE_COMMENTS);
32 onCreate(db);
33}
34
35public class Comment {
36private long id;
37private String comment;
38
39public long getId() {
40 return id;
41}
42
43public void setId(long id) {
44 this.id = id;
45}
46
47public String getComment() {
48 return comment;
49}
50
51public void setComment(String comment) {
52 this.comment = comment;
53}
54
55// Will be used by the ArrayAdapter in the ListView
56@Override
57public String toString() {
58 return comment;
59}
60 }
61
62 public class CommentsDataSource {
63
64// Database fields
65private SQLiteDatabase database;
66private DataBaseHelper dbHelper;
67private String[] allColumns = { DataBaseHelper.COLUMN_ID,
68 DataBaseHelper.COLUMN_COMMENT };
69
70public CommentsDataSource(Context context) {
71 dbHelper = new DataBaseHelper(context);
72}
73
74public void open() throws SQLException {
75 database = dbHelper.getWritableDatabase();
76}
77
78public void close() {
79 dbHelper.close();
80}
81
82public Comment createComment(String comment) {
83 ContentValues values = new ContentValues();
84 values.put(DataBaseHelper.COLUMN_COMMENT, comment);
85 long insertId = database.insert(DataBaseHelper.TABLE_COMMENTS, null,
86 values);
87 Cursor cursor = database.query(DataBaseHelper.TABLE_COMMENTS,
88 allColumns, DataBaseHelper.COLUMN_ID + " = " + insertId, null,
89 null, null, null);
90 cursor.moveToFirst();
91 Comment newComment = cursorToComment(cursor);
92 cursor.close();
93 return newComment;
94}
95
96public void deleteComment(Comment comment) {
97 long id = comment.getId();
98 System.out.println("Comment deleted with id: " + id);
99 database.delete(DataBaseHelper.TABLE_COMMENTS, DataBaseHelper.COLUMN_ID
100 + " = " + id, null);
101}
102
103public List<Comment> getAllComments() {
104 List<Comment> comments = new ArrayList<Comment>();
105
106 Cursor cursor = database.query(DataBaseHelper.TABLE_COMMENTS,
107 allColumns, null, null, null, null, null);
108
109 cursor.moveToFirst();
110 while (!cursor.isAfterLast()) {
111 Comment comment = cursorToComment(cursor);
112 comments.add(comment);
113 cursor.moveToNext();
114 }
115 // Make sure to close the cursor
116 cursor.close();
117 return comments;
118}
119
120private Comment cursorToComment(Cursor cursor) {
121 Comment comment = new Comment();
122 comment.setId(cursor.getLong(0));
123 comment.setComment(cursor.getString(1));
124 return comment;
125}
126 }
127
128public class WorkoutProgress extends ListActivity {
129private CommentsDataSource datasource;
130
131@Override
132
133public void onCreate(Bundle savedInstanceState) {
134 requestWindowFeature(Window.FEATURE_NO_TITLE);
135 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
136 super.onCreate(savedInstanceState);
137 setContentView(R.layout.progress);
138
139 datasource = new CommentsDataSource(this);
140 datasource.open();
141
142 List<Comment> values = datasource.getAllComments();
143
144 // Use the SimpleCursorAdapter to show the
145 // elements in a ListView
146 ArrayAdapter<Comment> adapter = new ArrayAdapter<Comment>(this,
147 android.R.layout.simple_list_item_1, values);
148 setListAdapter(adapter);
149}
150
151// Will be called via the onClick attribute
152// of the buttons in main.xml
153public void onClick(View view) {
154 @SuppressWarnings("unchecked")
155 ArrayAdapter<Comment> adapter = (ArrayAdapter<Comment>) getListAdapter();
156 Comment comment = null;
157 switch (view.getId()) {
158 case R.id.add:
159 String[] comments = new String[] { "Cool", "Very nice", "Hate it" };
160 int nextInt = new Random().nextInt(3);
161 // Save the new comment to the database
162 comment = datasource.createComment(""+49);
163 adapter.add(comment);
164 break;
165 case R.id.delete:
166 if (getListAdapter().getCount() > 0) {
167 comment = (Comment) getListAdapter().getItem(0);
168 datasource.deleteComment(comment);
169 adapter.remove(comment);
170 }
171 break;
172 }
173 adapter.notifyDataSetChanged();
174}
175
176@Override
177protected void onResume() {
178 datasource.open();
179 super.onResume();
180}
181
182@Override
183protected void onPause() {
184 datasource.close();
185 super.onPause();
186}
187
188datasource = new CommentsDataSource(this);